C++ 错误:使用未识别的类型';顶点';

C++ 错误:使用未识别的类型';顶点';,c++,C++,这是代码 class Vertex; class CPD { private: width; public: void initialize() { . . . } void updateTable(LinkedList<Vertex*>* parents) { node<Vertex *> *ptr = parents->getHead();

这是代码

class Vertex;

class CPD
{
private:
    width;

public:
    void initialize()
    {   .
        .
        .
    }

    void updateTable(LinkedList<Vertex*>* parents)
    {
        node<Vertex *> *ptr = parents->getHead();
        int W = 1;
        while (ptr)
        {
            W *= ((ptr->data)->getStates())->getSize();
            ptr = ptr->next;
        }
        width = W;
        initialize();
    }
};
类顶点;
类CPD
{
私人:
宽度
公众:
void initialize()
{   .
.
.
}
void updateTable(LinkedList*父级)
{
节点*ptr=parents->getHead();
int W=1;
while(ptr)
{
W*=((ptr->data)->getStates())->getSize();
ptr=ptr->next;
}
宽度=W;
初始化();
}
};

然而,在第一条语句中,我在while循环中遇到了一个“使用未定义类型‘Vertex’”错误,尽管我在开头给出了一个类Vertex原型。非常感谢您提供的帮助。

只要只给出Vertex的正向声明,编译器就不知道此类及其成员的任何信息。怎么可能呢

然而,您不需要这些详细信息来申报CPD。编译器只需知道类顶点的存在就可以理解函数签名

这样,就可以避免顶点和CPD的相互依赖性。正如@kec所指出的,解决方案是将updateTable()的定义移动到另一个文件中,其中包含顶点的完整定义

文件Vertex.hpp:

class Vertex {
  // declaration here
};
文件CPD.hpp:

class Vertex;

class CPD {
  void updateTable(LinkedList<Vertex*>* parents);
  // ...
};
类顶点;
类CPD{
void updateTable(LinkedList*父级);
// ...
};
文件CPD.cpp:

#include "Vertex.hpp"

void CPD::updateTable(LinkedList<Vertex*>* parents){
  // definition here
}
#包括“Vertex.hpp”
void CPD::updateTable(LinkedList*父级){
//这里的定义
}

您需要的是Vertex的完整定义,而不仅仅是一个正向声明。“Vertex”类本身使用“CPD”类,因此我将回到原点…
updateTable()
不应该是内联的。将其移动到实现文件中。那你就没事了。你的电脑不能编译。您的
宽度声明已损坏。把字型也放进去。