Apple Mach-O-Linker Error: Variable referenced from (line) not found in architecture x86-64

Apple Mach-O-Linker Error: Variable referenced from (line) not found in architecture x86-64

我正在为一个 class 项目编写 C++ 程序,在该项目中我将文件中的对象数据加载到列表中。我设置了它,以便像在成员函数中一样完成数据读取,然后将该对象推送到列表,并重复,直到没有更多数据要加载。

我收到此错误:

Undefined symbols for architecture x86_64:

"Product::Product(Product const&)", referenced from:

std::__1::list ::push_back(Product const&) in Week-2.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

主要功能:

Product temp;
list <Product> allProducts;

if (!temp.loadInitalData())
{
    cout << "There is no Product data available. Try a different option: ";
    cin >> choice;
    repeat = true;
}
else {
     while (temp.loadInitalData())
     {
         allProducts.push_back(temp);   //This is where I'm getting the error
     }
     temp.viewMenu();
     repeat = false;
}

产品的 loadInitalData() 函数:

bool Product::loadInitalData()
{
    bool moreData = true;
    fstream prodDatabase;
    prodDatabase.open("ProductDatabase.csv", ios::out|ios::in|ios::binary);
    if (!prodDatabase)
    {
        cout << "File could not be successfully opened\n";
        moreData = false;
    }
    else
    {
        moreData = loadInitialItemData(prodDatabase);
    }
    return moreData;
}

我在我的 main.cpp 文件中链接了 "Product.h",还有 .有什么建议吗?

您缺少 class 产品的复制构造函数。关于编译器何时提供一个规则,如下所述:

http://en.cppreference.com/w/cpp/language/copy_constructor

但如果复制 Product 对象是一项 non-trivial 操作,您可能仍然需要编写一个。

还可以考虑将您的列表设为产品 * 的列表(即指向产品的指针的列表)。这避免了复制,并且很可能是您真正想要的。