This is my code for an online shopping cart containing two items. I'm getting the following linker error.
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): undefined reference to `ItemToPurchase::SetPrice(int)
I get similar errors to all my functions in my public class. I double-checked all my parameters and made sure they had the matching data types and made sure I'd defined my functions. Here is my code. I'm fairly new to C++ so I'm just looking to learn and make sure this doesn't happen again.
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;
}
header file
#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