简体   繁体   English

如何将显式声明的构造函数分隔为.h和.cpp文件?

[英]How to separate constructor declared explicitly into .h & .cpp files?

Here is a piece of code which I found in OpenCV tutorial (source https://github.com/Itseez/opencv/blob/master/samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp ). 这是我在OpenCV教程中找到的一段代码(来源https://github.com/Itseez/opencv/blob/master/samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp )。 Because the code is all in one file I try to separate it into header and cpp. 因为代码都在一个文件中,所以我尝试将它分成header和cpp。 My problem is I cannot find out how to do it correctly not to generate error C4430: missing type specifier - int assumed. 我的问题是我无法找到如何正确地做到不生成错误C4430:缺少类型说明符 - 假设的int。 Note: C++ does not support default-int. 注意:C ++不支持default-int。

class MyData
{
public:
MyData() : A(0), X(0), id() {}
explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
{}
void write(FileStorage& fs) const //Write serialization for this class
{
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
}

public: // Data Members
int A;
double X;
string id;
};

So this is the part which I am stuck at: 所以这是我坚持的部分:

MyData() : A(0), X(0), id() {}
explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to 

A declaration (header file) which I tried: 我试过的声明(头文件):

class MyData
{
    public: // Data Members
        int A;
        double X;
        std::string id;

        MyData() ;
        explicit MyData(int);

    void write(cv::FileStorage& fs) const;
    void read(const cv::FileNode& node);
};

#endif

The thing in .cpp .cpp中的东西

MyData::MyData() : A(0), X(0), id() {}  // this is simple

But what next? 但接下来呢?

MyData::MyData(int) : 
    A(97), X(CV_PI), id("mydata1234") {};
MyData::write(cv::FileStorage& fs) const 
{ // ... it will stuck here C4430 
 }

using Visual Studio 2010 使用Visual Studio 2010

MyData::write(cv::FileStorage& fs) const 
{ // ... it will stuck here C4430 
 }

C4430 is "missing type specifier". C4430是“缺少类型说明符”。 It's because the MyData::write() implementation contains no return type. 这是因为MyData::write()实现不包含返回类型。 Only constructors should omit a return type. 只有构造函数应该省略返回类型。 Change it to void MyData::write(cv::FileStorage& fs) const to match its declaration. 将其更改为void MyData::write(cv::FileStorage& fs) const以匹配其声明。

To add a body for your write method outside of the class definition the format is : 要在类定义之外为write方法添加主体,格式为:

return-type classname::methodname(parameters)
{
}

So in this case: 所以在这种情况下:

void MyData::write(cv::FileStorage& fs) const 
{ 

}

In your hpp when you are declaring it should be something like this : 在你的hpp中,当你声明它应该是这样的:

class Name{
 .....some other functions  ....
void write(cv::FileStorage& fs);
};

and in your cpp you need first to include your hpp and then : 在您的cpp中,您首先需要包含您的hpp,然后:

void Name::write(cv::FileStorage& fs)
{
    .......body...
}

So first say the type of the funciton (in this case void ) then name of the class then :: and then the function name . 首先说明函数的类型(在本例中为void )然后是class名称::然后是函数名。

hope this helped :) 希望这有助于:)

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

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