1

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

1 Answer 1

0

Make sure SetPrice(int price) is defined in class ItemToPurchase in ItemToPurchase.h.

Sign up to request clarification or add additional context in comments.

5 Comments

I defined the functions in my header file before and I'm still getting those errors.
I included the header file in the original post.
What are your build commands? Is it possible you built main.c as C and ItemToPurchase.cpp as C++? Or did you only build main.c straight into an executable? You should compile both files to .o files using g++ -c -o <basename> basename.cpp and then link both of them together with something like g++ -o <executablefile> <basename1>.o <basename2>.
This is what I got in the terminal of VS code when I tried to run it 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()' I get similar errors for all my functions.
Like I was trying to say: g++ main.cpp -o main is wrong, as you compile only 1 of your sources and link to the final exe, so of course all the stuff from ItemToPurchase.cpp is missing in the final exe. If you really want to compile and link in one step you could try this: g++ main.cpp ItemToPurchase.cpp -o main

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.