【问题标题】:c++ vector problem; passing stuff by referencec++向量问题;通过引用传递东西
【发布时间】:2011-05-22 21:30:28
【问题描述】:

所以我检查了我的指针,我真的不确定这里出了什么问题。我通过引用修改它们的函数来传递 2 个向量向量。函数如下:

bool imageToTips( Mat& img,
  vector<vector<int> > & fingertips,
  vector<vector<Point> > & hand,
  double epsilon,
  double theta){
      //code 
}

这就是它的名字:

Mat depth = _depth;
Mat image = depth.clone();
Mat decon = depth.clone();
vector<vector<int> >  fingertips();
vector<vector<Point> >  hands();
double epsilon = 50;
double theta = 30;
if (fs::imageToTips(decon, fingertips, hands, epsilon, theta)) 
{
    drawContours(image, hands, -1, Scalar(255,0,0,0));
    imshow("KinectFingerProcessing", image);
    //more code
}

错误:

/FingerLocator2/main.cpp:47: error: invalid initialization of non-const reference of type 'std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&' from a temporary of type 'std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > (*)()'

/FingerLocator2/main.cpp:49: error: invalid initialization of reference of type 'const std::vector<std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >, std::allocator<std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > > > >&' from expression of type 'std::vector<std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >, std::allocator<std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > > > > (*)()'

我试过制作矢量指针,但没有奏效。我想我在某处或某处使用了临时变量……这里有什么问题?

【问题讨论】:

  • 哈哈,最常见的问题之一,但很难搜索...
  • @Xeo:除非你知道如何搜索 Most Vexing Parse(或者你的意思是,在代码中?一些编译器会帮助你 -Wall 或类似的)
  • @sehe:如果你知道最棘手的解析,你就不会遇到这个问题,甚至不会问这个问题。 ;)

标签: c++ pointers vector reference temporary


【解决方案1】:

您不需要 () 来默认构造变量,否则您正在定义一个函数。

vector<vector<int> >  fingertips;
vector<vector<Point> >  hands;
//                           ^ loose the ()s

【讨论】:

  • 语法 nit:“失去 ()s”,而不是“失去 ()s”。
  • @Rob: nit nit...'lose' 和 'loose' 在语法上没有关系...这是拼写问题(或者乐观地说,错字)
【解决方案2】:

你应该像这样初始化向量,没有'()':

vector<vector<int> >  fingertips;
vector<vector<Point> >  hands;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多