gpt4 book ai didi

c++ - 如何解决这个前向类声明问题?

转载 作者:行者123 更新时间:2023-12-04 12:15:11 25 4
gpt4 key购买 nike

我有以下情况:

#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;
}

一个所有者可以拥有多个项目,但每个项目还必须包含有关其所有者的信息。当我运行这个时,我得到

Error C2027 use of undefined type 'Item'



我认为这可以通过使用前向类声明来解决,但没有这样的运气。另外,我猜我的复制构造函数和赋值运算符还可以吗?

最佳答案

向前声明类型只是解决方案的一部分。当依赖项是循环的时,您还需要移动至少一种类型的函数的定义 - 通常放在 .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;
}

Try it yourself

此外,您 复制 项目进入所有者,因此当您打印原件时,它只打印“拥有的杯子”,而所有者“约翰”丢失。目前尚不清楚这应该以哪种方式修复 - 但可以例如。通过存储修复 std::shared_ptr相反,如果应该共享项目。

关于c++ - 如何解决这个前向类声明问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834238/

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