collect2:错误:ld返回1个退出状态未定义引用

问题描述 投票:0回答:1

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

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; 

}
c++ linker-errors
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.