【问题标题】:collect2: error: ld returned 1 exit status undefined referencescollect2:错误:ld 返回 1 退出状态未定义的引用
【发布时间】:2020-10-04 12:49:15
【问题描述】:

这是我的包含两件商品的在线购物车的代码。 我收到以下链接器错误。

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\chine\ AppData\Local\Temp\ccQnOWnO.o:main.cpp:(.text+0x121): 未定义引用 `ItemToPurchase::SetPrice(int)

我在公共课程中的所有函数都遇到了类似的错误。我仔细检查了所有参数,确保它们具有匹配的数据类型,并确保我定义了我的函数。这是我的代码。我对 C++ 还很陌生,所以我只是想学习并确保不会再发生这种情况。

ItemToPurchase.cpp

#include "ItemToPurchase.h"

ItemToPurchase::ItemToPurchase() {
   string itemName = "none"; 
   int itemPrice = 0; 
   int itemQuantity = 0;
}

string ItemToPurchase::GetName() {
   return itemName; 
}

int ItemToPurchase::GetPrice() {
   return itemPrice; 
}

int ItemToPurchase::GetQuantity() {
   return itemQuantity; 
}

void ItemToPurchase::SetName(const char* itemName) {
   this->itemName = itemName;
}

void ItemToPurchase::SetPrice(int price) {
   itemPrice = price; 
}

void ItemToPurchase::SetQuantity(int quantity) {
   itemQuantity = quantity; 
}

main.c

#include "ItemToPurchase.h"

int main() {
    ItemToPurchase Item1;  
    ItemToPurchase Item2; 
    string item1name; 
    int item1price;
    int item1quantity; 
    string item2name; 
    int item2price; 
    int item2quantity; 

    cout << "Item 1"; 
    cout << "Enter the item name: "; 
    getline(cin, item1name);

    item1name = Item1.GetName(); 
    Item1.SetName(item1name.c_str()); 

    cout << "Enter the item price: ";
    cin >> item1price;

    item1price = Item1.GetPrice(); 
    Item1.SetPrice(item1price);

    cout << "Enter the item quantity: ";
    cin >> item1quantity; 

    item1quantity = Item1.GetQuantity();
    Item1.SetQuantity(item1quantity);

    cout << "Item 2";
    cout << "Enter the item name: ";
    getline(cin, item2name); 

    item2name = Item2.GetName();
    Item2.SetName(item2name.c_str()); 

    cout << "Enter the item price: ";
    cin >> item2price; 

    item2price = Item2.GetPrice();
    Item2.SetPrice(item2price); 

    cout << "Enter the item quantity: ";
    cin >> item2quantity; 

    item2quantity = Item2.GetQuantity();
    Item2.SetQuantity(item2quantity);


    cout << "TOTAL COST" << endl; 
    cout << item1name << item1quantity << "@ " << "$" << item1price << "= " << "$" << item1price * item1quantity << endl; 
    cout << item2name << item2quantity << "@ " << "$" << item2price << "= " << "$" << item2price * item2quantity << endl;

    cout << "TOTAL: " << "$" << (item1price * item1quantity) + (item2price * item2quantity) << endl;


    return 0; 

}

头文件

#include <string>
#include <iostream> 
using namespace std;

class ItemToPurchase {
public: 
   ItemToPurchase();

   string GetName(); 
   int GetPrice();
   int GetQuantity(); 
   void SetName(const char* itemName);
   void SetPrice(int price);
   void SetQuantity(int quantity);

private:
   string itemName;
   int itemPrice;
   int itemQuantity;
};
#endif

【问题讨论】:

    标签: c++ linker-errors


    【解决方案1】:

    确保SetPrice(int price)class ItemToPurchase 中定义在ItemToPurchase.h 中。

    【讨论】:

    • 我之前在头文件中定义了函数,但仍然出现这些错误。
    • 我在原帖中包含了头文件。
    • 你的构建命令是什么?您是否可以将 main.c 构建为 C 并将 ItemToPurchase.cpp 构建为 C++?还是您只将main.c 直接构建为可执行文件?您应该使用g++ -c -o &lt;basename&gt; basename.cpp 将这两个文件编译为.o 文件,然后使用g++ -o &lt;executablefile&gt; &lt;basename1&gt;.o &lt;basename2&gt; 之类的东西将它们链接在一起。
    • 这是我尝试运行时在 VS 代码的终端中得到的结果 PS C:\Users\chine\OneDrive\Documents\LAB 5#1> cd "c:\Users\chine \OneDrive\Documents\LAB 5#1\" ; if ($?) { g++ main.cpp -o main } ; if ($?) { .\main } c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\chine\AppData\Local\Temp\ccHbOAwy.o:main.cpp:(.text+0x20): undefined reference to `ItemToPurchase::ItemToPurchase()' 我的所有函数都出现类似错误。跨度>
    • 就像我想说的那样:g++ main.cpp -o main 是错误的,因为您只编译了 1 个源代码并链接到最终的 exe,所以 ItemToPurchase.cpp 中的所有内容当然都在最终的exe。如果你真的想一步编译和链接,你可以试试这个:g++ main.cpp ItemToPurchase.cpp -o main
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2021-08-04
    • 2013-06-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多