结构构造函数中不允许使用不完整类型

Hun*_*oan 2 c++

尝试这样做时出现此错误:

#include <iostream>
using namespace std;
struct VertexStatus
{
private:
    int _CurrentStatus;

public:
    static VertexStatus Discovered = new VertexStatus(1); //incomplete type is not allowed
    VertexStatus(int iStatus)
    {
        this->_CurrentStatus = iStatus;
    }
};
Run Code Online (Sandbox Code Playgroud)

有什么不对?

NPE*_*NPE 7

将初始化移出类体:

struct VertexStatus
{
    ...
    static VertexStatus Discovered; // declaration
    ...
};

VertexStatus VertexStatus::Discovered(1); // definition (with initializer)
Run Code Online (Sandbox Code Playgroud)

如果在头文件中声明了类,则将最后一行(定义)放入相应的.cpp文件中.

请注意我已删除new:它返回一个指针,此代码中没有指针.