在这个范围内没有声明吗?

时间:2022-06-06 06:54:11

I am new in C++ and working on a project with QT. I created a header file called imageconvert.h which is as follow:

我是c++的新成员,正在使用QT进行一个项目。我创建了一个名为imageconvert的头文件。h,如下:

class ImageConvert
{

private:
IplImage *imgHeader;
uchar* newdata;

public:

ImageConvert();
~ImageConvert();
IplImage* QImage2IplImage(QImage *qimg);
QImage* IplImage2QImage(IplImage *iplImg);
};

also I defined those public methods in imageconvert.cpp file.

我还在imageconvert中定义了这些公共方法。cpp文件。

Now, I want to call QImage2IplImage and IplImage2QImage from other cpp file. So i include imageconvert.h in that CPP file and called those two functions.

现在,我想从其他cpp文件中调用QImage2IplImage和IplImage2QImage。所以我包括imageconvert。h在CPP文件中,叫做这两个函数。

it gives the the following errors:

它给出了以下错误:

error: 'QImage2IplImage' was not declared in this scope
error: 'IplImage2QImage' was not declared in this scope

Any help would be greatly appreciated.

非常感谢您的帮助。

2 个解决方案

#1


5  

The functions you've defined are member functions of the ImageConvert class. You need an instance of that class to be able to call them.

您定义的函数是ImageConvert类的成员函数。您需要该类的一个实例来调用它们。

Something like:

喜欢的东西:

ImageConvert ic;
ic.QImage2IplImage(your_QImage_object);

If you don't need state to do the conversion, you should make those helper functions static. Then you can call them with:

如果您不需要状态来执行转换,则应该使这些helper函数保持静态。然后你可以打电话给他们:

ImageConvert::QImage2IplImage(your_QImage_object);

without first creating an instance of ImageConvert. But please note that you will not be able to use imgHeader or newData in those static functions - they are member variables, only usable within an instance of that class.

没有首先创建一个ImageConvert实例。但是请注意,您将不能在这些静态函数中使用imgHeader或newData——它们是成员变量,只能在该类的一个实例中使用。

You could also remove these functions from your class and put them in a namespace.

您还可以从类中删除这些函数,并将它们放在一个名称空间中。

#2


3  

Your question...

How exactly do you call those functions? Given your ImageConverter class, this is how you should be doing it:

你怎么称呼这些函数?考虑到您的ImageConverter类,您应该这样做:

// First create a new converter
ImageConverter conv;

IplImage* ipl = conv.QImage2IplImage(qimg);
qimg = conv.IplImage2QImage(ipl);

... And some advice on using classes

Do you by any chance come from a Java or C# background? If so, you should know that in C++ you can also have free functions (that don't belong to any class). You should only use classes when you need to abstract a certain (real world) concept, and not simply as a way to group functions:

你是否有机会来自Java或c#背景?如果是这样,您应该知道在c++中,您还可以拥有*函数(不属于任何类)。当您需要抽象一个特定的(真实的世界)概念时,您应该只使用类,而不是简单地将其作为组函数的一种方式:

// image_converter.h
IplImage* QImage2IplImage(const QImage* qimg);
QImage* IplImage2QImage(const IplImage* iplImg);

// someother.cpp
IplImage* ipl = QImage2IplImage(qimg);
qimg = IplImage2QImage(ipl);

Notice I added const to the function parameters — it's a good thing to be const correct. Additionaly, you can group your functions in a namespace:

注意,我在函数参数中添加了const,这是一个很好的常量。另外,您可以将函数分组到一个名称空间中:

// image_converter.h
namespace converter
{
    IplImage* QImage2IplImage(const QImage* qimg);
    QImage* IplImage2QImage(const IplImage* iplImg);
}

// someother.cpp
IplImage* ipl = converter::QImage2IplImage(qimg);
qimg = converter::IplImage2QImage(ipl);

#1


5  

The functions you've defined are member functions of the ImageConvert class. You need an instance of that class to be able to call them.

您定义的函数是ImageConvert类的成员函数。您需要该类的一个实例来调用它们。

Something like:

喜欢的东西:

ImageConvert ic;
ic.QImage2IplImage(your_QImage_object);

If you don't need state to do the conversion, you should make those helper functions static. Then you can call them with:

如果您不需要状态来执行转换,则应该使这些helper函数保持静态。然后你可以打电话给他们:

ImageConvert::QImage2IplImage(your_QImage_object);

without first creating an instance of ImageConvert. But please note that you will not be able to use imgHeader or newData in those static functions - they are member variables, only usable within an instance of that class.

没有首先创建一个ImageConvert实例。但是请注意,您将不能在这些静态函数中使用imgHeader或newData——它们是成员变量,只能在该类的一个实例中使用。

You could also remove these functions from your class and put them in a namespace.

您还可以从类中删除这些函数,并将它们放在一个名称空间中。

#2


3  

Your question...

How exactly do you call those functions? Given your ImageConverter class, this is how you should be doing it:

你怎么称呼这些函数?考虑到您的ImageConverter类,您应该这样做:

// First create a new converter
ImageConverter conv;

IplImage* ipl = conv.QImage2IplImage(qimg);
qimg = conv.IplImage2QImage(ipl);

... And some advice on using classes

Do you by any chance come from a Java or C# background? If so, you should know that in C++ you can also have free functions (that don't belong to any class). You should only use classes when you need to abstract a certain (real world) concept, and not simply as a way to group functions:

你是否有机会来自Java或c#背景?如果是这样,您应该知道在c++中,您还可以拥有*函数(不属于任何类)。当您需要抽象一个特定的(真实的世界)概念时,您应该只使用类,而不是简单地将其作为组函数的一种方式:

// image_converter.h
IplImage* QImage2IplImage(const QImage* qimg);
QImage* IplImage2QImage(const IplImage* iplImg);

// someother.cpp
IplImage* ipl = QImage2IplImage(qimg);
qimg = IplImage2QImage(ipl);

Notice I added const to the function parameters — it's a good thing to be const correct. Additionaly, you can group your functions in a namespace:

注意,我在函数参数中添加了const,这是一个很好的常量。另外,您可以将函数分组到一个名称空间中:

// image_converter.h
namespace converter
{
    IplImage* QImage2IplImage(const QImage* qimg);
    QImage* IplImage2QImage(const IplImage* iplImg);
}

// someother.cpp
IplImage* ipl = converter::QImage2IplImage(qimg);
qimg = converter::IplImage2QImage(ipl);