1

this is for C++ homework for college so I'm still a beginner but I have never seen this error before. The program is just a simple make a class, make two class objects and spit out the attributes of the class to the user all attributes must be checked by Set Get functions. If someone could explain this error and teach me how to prevent/fix it, it would be much appreciated, thank you!

This is the full error: Error 1 error LNK2019: unresolved external symbol "public: __thiscall invoice::invoice(class std::basic_string,class std::allocator >,class std::basic_string,class std::allocator >,int,int)" (??0invoice@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0HH@Z) referenced in function _main C:\Users\PBirkholtz001\Desktop\Exam Finals\invoicerExamQuest1\invoicerExamQuest1\Source.obj invoicerExamQuest1

Error 2 error LNK1120: 1 unresolved externals C:\Users\PBirkholtz001\Desktop\Exam Finals\invoicerExamQuest1\Debug\invoicerExamQuest1.exe invoicerExamQuest1

This is my header file:

 #ifndef INVOICE_H
   #define INVOICE_H

   #include <iostream>
   #include <string>
using namespace std;

class invoice{

private:
    // Variables for the class
    string partNum;
    string desc;
    int quant;
    int price;

public:
    //constructor
    invoice(string partNum = "f000", string desc = "HardWare", int quant = 1, int price = 0);

//////////////////get set functions////////////////////////////////
    void setpartnum(string a){
        partNum = a;
    }
    string getpartnum(){
        return partNum;
    }
//////////////////////////////////////////////////
    void setdesc(string b){
        desc = b;
    }
    string getdesc(){
        return partNum;
    }
/////////////////////////////////////////////////
    void setquant(int x){
        quant = x;
    }
    int getquant(){
        return quant;
    }
////////////////////////////////////////////////
    void setprice(int y){
        quant = y;
    }
    int getprice(){
        return price;
    }
/////////////////Total function//////////////////////////////
    int invoiceAmount(){
        return quant * price;
    }
///////////////////////////////////////////////
};
#endif

this is my source file:

#include <iostream>
#include "Header.h"
using namespace std;

int main(){

    invoice invoice1;

    invoice1.setpartnum("SD001");
    invoice1.setdesc("PNY 100Gb Solid State Drive");
    invoice1.setprice(120);
    invoice1.setquant(1);

    cout << "you have ordered: " << invoice1.getquant() << " " << invoice1.getpartnum() << endl;
    cout << " which will cost: $" << invoice1.getprice() << " your total comes to: $" << invoice1.invoiceAmount() << endl;

    system("pause");
}
Hopelessdecoy
  • 119
  • 1
  • 2
  • 10
  • Comment: You have created the empty constructor? Why do not you create the methods in a "CPP" file? eg. `header.h` and `header.cpp` – Protomen Dec 10 '14 at 19:17

1 Answers1

2
invoice(string partNum = "f000", string desc = "HardWare", int quant = 1, int price = 0);

You declare this constructor to be part of the class, but you do not implement it anywhere. You need to provide an implementation of it, because you implicitly use it on the line invoice invoice1;.

You could implement it by changing this line to:

invoice(string partNum = "f000", string desc = "HardWare", int quant = 1, int price = 0)
    : partNum(partNum), desc(desc), quant(quant), price(price) { }
cdhowie
  • 158,093
  • 24
  • 286
  • 300