C++ 如何解决这个前向类声明问题?
我的情况如下:C++ 如何解决这个前向类声明问题?,c++,forward-declaration,C++,Forward Declaration,我的情况如下: #include <iostream> #include <vector> #include <string> using namespace std; // Forward declarations class Owner; class Item; class Owner { public: string name; vector<Item> item; Owner(string _name, init
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Forward declarations
class Owner;
class Item;
class Owner {
public:
string name;
vector<Item> item;
Owner(string _name, initializer_list<Item> _item) : name(_name), item(_item) {
for (size_t i = 0; i < item.size(); i++)
item[i].owner = this;
}
// change the owner when copying...
Owner(const Owner& o) {
name = o.name;
item = o.item;
for (size_t i = 0; i < item.size(); i++)
item[i].owner = this; // is this OK?
}
Owner& operator = (const Owner& o) {
name = o.name;
item = o.item;
for (size_t i = 0; i < item.size(); i++)
item[i].owner = this;
return *this;
}
};
class Item {
public:
string name;
Owner* owner;
Item(string _name) : name(_name) {}
string getInfo() {
return name + " owned by " + owner->name;
}
};
int main(){
Item Cup("Cup"), Plate("Plate");
Owner John("John", { Cup, Plate });
cout << Cup.getInfo() << endl; // expecting "Cup owned by John"
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//转发声明
类所有者;
类别项目;
类所有者{
公众:
字符串名;
向量项;
所有者(字符串名称、初始值设定项列表项目):名称(\u名称)、项目(\u项目){
对于(size_t i=0;i名称;
}
};
int main(){
项目杯(“杯”)、盘(“盘”);
所有者约翰(“约翰”{Cup,Plate});
cout当您使用std::vector
时,Item
必须是一个完整的类型。在这种情况下,您只在Item
内部使用所有者*
,因此Item
应该放在第一位。
由于您在getInfo()
中取消引用了Owner*
,因此其定义应位于Owner
类之后:
class Owner;
class Item {
// ...
std::string getInfo() const;
};
class Owner {
// ...
};
std::string Item::getInfo() const
{
// return ...
}
前向声明类型只是解决方案的一部分。当依赖项是循环的时,您还需要移动至少一种类型的函数定义-通常放在.cpp文件中(但不必如示例中所示)
工作示例:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Forward declarations
class Owner;
class Item {
public:
string name;
Owner* owner;
Item(string _name) : name(_name) {}
string getInfo();//<-- only declaration now
};
class Owner {
public:
string name;
vector<Item> item;
Owner(string _name, initializer_list<Item> _item) : name(_name), item(_item) {
for (size_t i = 0; i < item.size(); i++)
item[i].owner = this;
}
// change the owner when copying...
Owner(const Owner& o) {
name = o.name;
item = o.item;
for (size_t i = 0; i < item.size(); i++)
item[i].owner = this; // is this OK?
}
Owner& operator = (const Owner& o) {
name = o.name;
item = o.item;
for (size_t i = 0; i < item.size(); i++)
item[i].owner = this;
return *this;
}
};
//or put in .cpp file
string Item::getInfo() {
return name + " owned by " + owner->name;
}
int main(){
Item Cup("Cup"), Plate("Plate");
Owner John("John", { Cup, Plate });
cout << Cup.getInfo() << endl; // expecting "Cup owned by John"
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//转发声明
类所有者;
类项目{
公众:
字符串名;
业主*业主;
项(字符串_name):名称(_name){}
字符串getInfo();//名称;
}
int main(){
项目杯(“杯”)、盘(“盘”);
所有者约翰(“约翰”{Cup,Plate});
那么,我是否可以得到这样一条消息:所有者未在函数getInfo中定义。@Tracerinitializer\u list
复制。您正在尝试关联对象,而不是复制它们。这是一个新问题……我不会使项
和所有者
可复制(可能是可移动的,但我甚至不确定)。您需要在扩展名为.cpp
和.h
的不同文件中创建这些类。在项目
的头文件中,您可以使用转发声明,在cpp
文件中包括所有者.h
中,您需要包括项目.h
@Raffallo,它们不需要位于不同的位置nt文件。成员函数的定义需要遵循每个类的定义,但这可以在一个文件中完成,但在这种情况下不能输出任何内容…?@Tracer那可能是另一个错误。我得到了输出“Cup owned by”-如此失踪John@Tracer,我现在明白了,您将对象复制到owner中,并且打印是原始的,无法获得任何输出。事实上,在调试中,我在“基本字符串和附加(常量基本字符串和右键)”中得到以下异常“异常引发:读取访问冲突。”在xstring头中。@Tracer听起来不相关-可能是您的构建中的一个错误,这里是一个工作示例