简体   繁体   English

从对象打印 2D int 向量

[英]Printing a 2D int vector from an object

So I'm pretty sure it's having issues because it's trying to print the pointer instead of the value, but I don't know how to fix it.所以我很确定它有问题,因为它试图打印指针而不是值,但我不知道如何修复它。

class cName {
private:
    std::vector< std::vector<int>> *vect2d;
public:
    // initializes vector that contains x number of vectors
    // that each contain y number of ints
    cName(int x,int y); 
    void printVector(int);
}

void cName::printVector(int x) {
    for(int i=0; i<x; i++) {
        //have to get size because 
        // the number of ints in the vector will change
        for(int j=0; j< this->vect2d[i].size(); j++) 
            std::cout << this->vect2d[i][j]<<" ";
        std::cout<<"\n";
    }
}

I'm having issues printing a 2d vector I use in a class I'm making.我在打印我正在制作的课程中使用的二维矢量时遇到问题。 I get an error that says:我收到一条错误消息:

cannot bind 'std::ostream {aka std::basic_ostream<char>' 
lvalue to 'std::basic_ostream<char>&&'

Question:题:

Could someone explain me why is throwing me that error and help me fix it?有人可以向我解释为什么向我抛出那个错误并帮助我修复它吗?

The vect2d member is a pointer to a vector of vectors of ints. vect2d成员是一个指向整数向量向量的指针。 You don't really need a pointer here, just use a vector of vectors of ints.您在这里并不真正需要指针,只需使用整数向量的向量即可。

Your usage of this pointer does not produce any immediate errors because the subscript operator array[index] can be used on pointers.使用此指针不会立即产生任何错误,因为下标运算符array[index]可用于指针。 If you are not sure that your code is correct, prefer to use the range-checked .at(index) method for std::vector instances.如果您不确定您的代码是否正确,请首选对std::vector实例使用范围检查的.at(index)方法。 Using an explicit method would have pointed you to your error, since there is no .at(index) method for pointers.使用显式方法会指向您的错误,因为没有.at(index)指针方法。

What the compiler currently sees when you call this->vect2d[i].size() is:当您调用this->vect2d[i].size()时,编译器当前看到的是:

  • this->vect2d of type vector<vector<int>>* , a complicated way of spelling vect2d . this->vect2d类型vector<vector<int>>* ,一种复杂的拼写方法vect2d Note that this is a pointer type.请注意,这是一个指针类型。
  • this->vect2d[i] of type vector<vector<int>> , which is equivalent to *(vect2d + i) , but not to (*vect2d)[i] or vect2d->at(i) ! this->vect2d[i]类型为vector<vector<int>> ,等同于*(vect2d + i) ,但不等同于(*vect2d)[i]vect2d->at(i) Note that this is not a pointer type, but still two nested vectors.请注意,这不是指针类型,而是两个嵌套向量。
  • Therefore, the .size() is called on the vector that is i vector sizes away from your outer *vect2d container.因此, .size()被调用的是矢量i从你的外在载体尺寸远*vect2d容器。 Quite likely, this is invalid memory and could segfault.很可能,这是无效的内存,可能会出现段错误。

When you later do vect2d[i][j] , that is actually equivalent to *(vect2d + i)[j] which should behave the same as (vect2d + i)->at(j) .当您稍后执行vect2d[i][j] ,这实际上等效于*(vect2d + i)[j] ,其行为应与(vect2d + i)->at(j) But it is not vect2d->at(i).at(j) !但它不是vect2d->at(i).at(j) Notably, it is of type vector<int> rather than int .值得注意的是,它的类型是vector<int>而不是int That is the cause of your error message: there's no available operator<< to print vectors, so the compiler produces that quite incomprehensible error.这就是您的错误消息的原因:没有可用的operator<<来打印向量,因此编译器会产生非常难以理解的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM