问一下c++赋值的有关问题,这个应该如何写
问一下c++赋值的问题,这个应该怎么写?
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2109: 下标要求数组或指针类型
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2819: “rgb”类型没有重载成员“operator ->”
1> e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明
1> 是否改用“.”?
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2232: “->rgb::b”: 左操作数有“struct”类型,使用“.”
1>e:\code\c++\graph_segmentation\main.cpp(83): error C2109: 下标要求数组或指针类型
1>e:\code\c++\graph_segmentation\main.cpp(83): error C2819: “rgb”类型没有重载成员“operator ->”
1> e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明
1> 是否改用“.”?
1>e:\code\c++\graph_segmentation\main.cpp(83): error C2232: “->rgb::g”: 左操作数有“struct”类型,使用“.”
1>e:\code\c++\graph_segmentation\main.cpp(84): error C2109: 下标要求数组或指针类型
1>e:\code\c++\graph_segmentation\main.cpp(84): error C2819: “rgb”类型没有重载成员“operator ->”
1> e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明
1> 是否改用“.”?
1>e:\code\c++\graph_segmentation\main.cpp(84): error C2232: “->rgb::r”: 左操作数有“struct”类型,使用“.”
1>
1>生成失败。
1>
1>已用时间 00:00:00.64
------解决思路----------------------
(seg->access)[i][j]->b; 先加上括号
------解决思路----------------------
编译器说的很明白了呀
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2232: “->rgb::b”: 左操作数有“struct”类型,使用“.”
-> 改成 . 呀
access 的类型 是 T**, 也就是 struct rgb **, 所以 access[i][j]的类型就是 struct rgb呀,引用成员用.啊
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j]->b;
改成
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j].b;
------解决思路----------------------
Mat seg_result(seg->w,seg->h,CV_8UC3); 是啥类型,
貌似提示的是等号左边的问题呀,
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j].b;
"下标要求数组或指针类型" 指的是等号左边的部分吧?
seg_result.at<uchar>(i,j)[0]
/*
Copyright (C) 2006 Pedro Felzenszwalb
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* a simple image class */
#ifndef IMAGE_H
#define IMAGE_H
#include <cstring>
template <class T>
class image
{
public:
/* create an image */
image(const int width, const int height, const bool init = true);
/* delete an image */
~image();
/* init an image */
void init(const T &val);
/* copy an image */
image<T> *copy() const;
/* get the width of an image. */
int width() const { return w; }
/* get the height of an image. */
int height() const { return h; }
/* image data. */
T *data;
/* row pointers. */
T **access;
public:
int w, h;
};
/* use imRef to access image data. */
#define imRef(im, x, y) (im->access[y][x])
/* use imPtr to get pointer to image data. */
#define imPtr(im, x, y) &(im->access[y][x])
template <class T>
image<T>::image(const int width, const int height, const bool init)
{
w = width;
h = height;
data = new T[w * h]; // allocate space for image data
access = new T*[h]; // allocate space for row pointers
// initialize row pointers
for (int i = 0; i < h; i++)
access[i] = data + (i * w);
if (init)
memset(data, 0, w * h * sizeof(T));
}
template <class T>
image<T>::~image()
{
delete [] data;
delete [] access;
}
template <class T>
void image<T>::init(const T &val)
{
T *ptr = imPtr(this, 0, 0);
T *end = imPtr(this, w-1, h-1);
while (ptr <= end)
*ptr++ = val;
}
template <class T>
image<T> *image<T>::copy() const
{
image<T> *im = new image<T>(w, h, false);
memcpy(im->data, data, w * h * sizeof(T));
return im;
}
#endif
int main(int argc, char **argv)
{
//if (argc != 6)
//{
//fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);
//return 1;
//}
//sigma 0.5 k 500 min 50
//参数说明http://cs.brown.edu/~pff/segment/
float sigma = 0;//atof(argv[1]);
float k = 0;//atof(argv[2]);
int min_size = 0;//atoi(argv[3]);
cin>>sigma;
cin>>k;
cin>>min_size;
printf("loading input image.\n");
char *image_input_name = "beach.ppm";
char *image_output_name ="output1.ppm";
image<rgb> *input = loadPPM(image_input_name);
printf("processing\n");
int num_ccs;
image<rgb> *seg = segment_image(input, sigma, k, min_size, &num_ccs);
savePPM(seg, image_output_name);
Mat seg_result(seg->w,seg->h,CV_8UC3);
for (int i = 0 ;i < seg->w;i++)
{
for (int j = 0 ; j < seg->h ;j++)
{
//uchar temp = 8<<seg->access[i][j];
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j]->b;
seg_result.at<uchar>(i,j)[1] = (char)seg->access[i][j]->g;//y不知道怎赋值了
seg_result.at<uchar>(i,j)[2] = (char)seg->access[i][j]->r;
}
}
printf("got %d components\n", num_ccs);
printf("done! uff...thats hard work.\n");
return 0;
}
Mat seg_result(seg->w,seg->h,CV_8UC3);
for (int i = 0 ;i < seg->w;i++)
{
for (int j = 0 ; j < seg->h ;j++)
{
//uchar temp = 8<<seg->access[i][j];
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j]->b;//这块应该怎么写?
seg_result.at<uchar>(i,j)[1] = (char)seg->access[i][j]->g;
seg_result.at<uchar>(i,j)[2] = (char)seg->access[i][j]->r;
}
}
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2109: 下标要求数组或指针类型
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2819: “rgb”类型没有重载成员“operator ->”
1> e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明
1> 是否改用“.”?
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2232: “->rgb::b”: 左操作数有“struct”类型,使用“.”
1>e:\code\c++\graph_segmentation\main.cpp(83): error C2109: 下标要求数组或指针类型
1>e:\code\c++\graph_segmentation\main.cpp(83): error C2819: “rgb”类型没有重载成员“operator ->”
1> e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明
1> 是否改用“.”?
1>e:\code\c++\graph_segmentation\main.cpp(83): error C2232: “->rgb::g”: 左操作数有“struct”类型,使用“.”
1>e:\code\c++\graph_segmentation\main.cpp(84): error C2109: 下标要求数组或指针类型
1>e:\code\c++\graph_segmentation\main.cpp(84): error C2819: “rgb”类型没有重载成员“operator ->”
1> e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明
1> 是否改用“.”?
1>e:\code\c++\graph_segmentation\main.cpp(84): error C2232: “->rgb::r”: 左操作数有“struct”类型,使用“.”
1>
1>生成失败。
1>
1>已用时间 00:00:00.64
------解决思路----------------------
(seg->access)[i][j]->b; 先加上括号
------解决思路----------------------
编译器说的很明白了呀
1>e:\code\c++\graph_segmentation\main.cpp(82): error C2232: “->rgb::b”: 左操作数有“struct”类型,使用“.”
-> 改成 . 呀
access 的类型 是 T**, 也就是 struct rgb **, 所以 access[i][j]的类型就是 struct rgb呀,引用成员用.啊
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j]->b;
改成
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j].b;
------解决思路----------------------
Mat seg_result(seg->w,seg->h,CV_8UC3); 是啥类型,
貌似提示的是等号左边的问题呀,
seg_result.at<uchar>(i,j)[0] = (char)seg->access[i][j].b;
"下标要求数组或指针类型" 指的是等号左边的部分吧?
seg_result.at<uchar>(i,j)[0]