gpt4 book ai didi

c++ - 指向对象的指针的未声明标识符 vector

转载 作者:行者123 更新时间:2023-11-28 03:28:47 25 4
gpt4 key购买 nike

错误:Cell.h 的第 12 行:“Actor”未声明的标识符。

如果我尝试在它上面转发声明,它说有一个重新定义。我该怎么办?

Actor.h:

#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;

class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();

};
#endif

单元格.h:

#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;

class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};

#endif

单元格.cpp:

#include "Cell.h"
#include <vector>

vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}

我还能做什么?

最佳答案

Actor.h 中删除 #include "Cell.h",您就可以开始了。

一般来说,在可能的地方优先使用前向声明,在必须的地方包含。我还将 Cell.h 中的 #include "Actor.h" 替换为前向声明:class Actor;

如果需要,您可以在 cpp 文件中包含标题。

关于c++ - 指向对象的指针的未声明标识符 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13198657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com