【问题标题】:Non Static Member Reference非静态成员参考
【发布时间】:2013-11-14 19:28:34
【问题描述】:

我在下面的代码中有一个错误:-

int main() {

cv::Mat image = imread("file.jpg", CV_LOAD_IMAGE_GRAYSCALE);
uint8_t* pixelPtr = (uint8_t*)image.data;
int cn = image.channels();
 for(int i = 0; i < image.rows; i++)
{
    for(int j = 0; j < image.cols; j += cn)
    {
        typedef Scalar_<uint8_t> bgrPixel;
        bgrPixel.val[0] = pixelPtr[i*image.cols*cn + j*cn + 0]; // B
        bgrPixel.val[1] = pixelPtr[i*image.cols*cn + j*cn + 1]; // G
        bgrPixel.val[2] = pixelPtr[i*image.cols*cn + j*cn + 2]; // R

    // do somethin
    }
}
}

VS10 下划线 val[0] 并表示非静态成员引用必须与特定对象相关。但它是 - 它与 bgrPixel 相关,不是吗?

【问题讨论】:

  • 您可能需要至少显示更多的上下文,以便任何人说得更有意义。
  • 如果编译器不这么认为,我们有什么不同意的呢?你能显示更多代码吗?
  • 我现在将添加更多代码......一秒钟
  • 好的,删除typedef这个词,看看会发生什么。
  • @RogerRowland 天才!谢谢!为什么会这样?

标签: c++ visual-studio-2010 opencv


【解决方案1】:

更长的解释:

下面一行:

typedef Scalar_<uint8_t> bgrPixel;

声明一个别名 - brgPixel - 可以用来代替 Scalar_&lt;uint8_t&gt; - 它实际上只是节省了输入并使代码更简洁、更易于阅读。此外 - 在其他情况下 - 它允许您通过修改 typedef 来更改该类型,而不是对变量类型本身进行查找/替换。

它没有声明任何 of 类型的变量。

因此,您可以删除 typedef 并声明一个变量:

Scalar_<uint8_t> bgrPixel;
bgrPixel.val[0] = pixelPtr[i*image.cols*cn + j*cn + 0]; 

或者,您可以使用typedef 作为变量类型:

typedef Scalar_<uint8_t> bgrPixel;
bgrPixel pPixel;
pPixel.val[0] = pixelPtr[i*image.cols*cn + j*cn + 0]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-31
    • 2016-01-21
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 2012-10-20
    相关资源
    最近更新 更多