C++ collect2:错误:ld返回了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.cppC++ collect2:错误:ld返回了1个退出状态未定义的引用,c++,linker-errors,C++,Linker Errors,这是我的代码,用于包含两个项目的在线购物车。 我收到以下链接器错误 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)的未定义引用 在我的公共类中,我的所有函数都会出现类似的错误。我仔细检查了
#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;
}
#包括“itemtopourchase.h”
int main(){
项目采购项目1;
项目采购项目2;
字符串item1name;
国际项目1价格;
int item1数量;
字符串item2name;
int item2price;
int item2数量;
计算项目1的数量;
item1quantity=Item1.GetQuantity();
项目1.设置数量(项目1数量);
数量不能超过2项;
item2quantity=Item2.GetQuantity();
项目2.设置数量(项目2数量);
无法确保SetPrice(intprice)
在itemtopourchase.h中的class itemtopourchase
中定义SetPrice(intprice)确保SetPrice(intprice)<代码> > <代码> > ITEMtoURSUTH.H./C> > /P>我在头文件中定义了函数,我仍然得到这些错误。我把头文件包含在原始POST中。你的构建命令是什么?你可以构建<代码> Meal.C.<代码>作为C和<代码> ITEMPururChest.CPP < /C>作为C++吗?或者你只是直接将main.c
编译成一个可执行文件吗?你应该使用g++-c-o basename.cpp
将这两个文件编译成.o
文件,然后用g++-o.o
之类的东西将它们链接在一起。这就是我在VS代码的终端运行PS c:\Users\chine\OneDrive\D时得到的结果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):未定义对'itemtophase::topurchase的引用'我的所有函数都会出现类似的错误。就像我想说的:g++main.cpp-o main
是错误的,因为您只编译了一个源代码并链接到最终的exe,所以当然,itemtopourchase.cpp
中的所有内容都会在最终的exe中丢失。如果您真的想一步编译并链接,您可以尝试一下:我以前定义了头文件中的函数,我仍然在得到这些错误。我把头文件包含在原始的POST中。你的构建命令是什么?你有可能把代码< > Meal.C < /> >作为C和<代码> ITEMPururChest.CPP <代码>作为C++?还是只建立了代码>主体?您应该使用g++-c-o basename.cpp
将这两个文件编译成.o
文件,然后用g++-o.o
之类的东西将它们链接在一起。这是我在尝试运行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):未定义对`ItemToPurchase::itemtourchase的引用'我的所有函数都会出现类似的错误。就像我想说的:g++main.cpp-o main
是错误的,因为您只编译了一个源代码并链接到最终的exe,所以当然,itemtopourchase.cpp
中的所有内容都会在最终的exe中丢失。如果您真的想一步编译并链接,您可以尝试一下:g++main.cpp itemtopourchase.cpp-o main
#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