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

标签 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, 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/

相关文章:

c++ - 为什么在使用类型作为字段时不能推迟不完整类型(前向声明)的大小计算?

c++ - sprintf_s 崩溃

C++:我正在解决一个问题,突然我注意到编译的代码没有返回语句

c++ - 通用 Windows 平台 Direct3D C++ 应用程序上的 60 FPS 游戏循环

C++ 错误 : explicit qualification in declaration

c++ - 装饰者模式中的装饰顺序

c++ - 我如何克服这个前向声明错误?

c++-cli - 如何在C++/CLI中转发声明属性?

C++ 前向声明和文件设计

c++ - 另一个函数内的函数前向声明