collect2: error: ld returned 1 exit status undefined references

collect2: error: ld returned 1 exit status undefined references

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

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)

的引用

我的 public class 中的所有函数都出现了类似的错误。我仔细检查了我的所有参数并确保它们具有匹配的数据类型并确保我已经定义了我的函数。这是我的代码。我是 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

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