C++错误error LNK2019 : unresolved external symbol,语法错误?

标签 c++ class linker-errors

<分区>

这是大学 C++ 作业,所以我还是初学者,但我以前从未见过这个错误。该程序只是简单的制作一个类,制作两个类对象并将类的属性吐出给用户所有属性都必须通过Set Get函数进行检查。如果有人可以解释这个错误并教我如何预防/修复它,将不胜感激,谢谢!

这是完整的错误: 错误 1 ​​error LNK2019: 未解析的外部符号“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) 在函数_main C:\Users\PBirkholtz001\Desktop 中引用\Exam Finals\invoicerExamQuest1\invoicerExamQuest1\Source.obj invoicerExamQuest1

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

这是我的头文件:

 #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

这是我的源文件:

#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");
}

最佳答案

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

您将此构造函数声明为类的一部分,但您并未在任何地方实现它。您需要提供它的实现,因为您在行 invoice invoice1; 中隐含地使用了它。

您可以通过将此行更改为:

invoice(string partNum = "f000", string desc = "HardWare", int quant = 1, int price = 0)
    : partNum(partNum), desc(desc), quant(quant), price(price) { }

关于C++错误error LNK2019 : unresolved external symbol,语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409015/

相关文章:

来自 C++ 程序员的 Java 问题

python - 最佳实践 python 类

swift - 为什么 forEach 中的迭代器对于结构来说是不可变的,但对于类来说是可变的?

c++ - 错误 C2679 : binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

c++ - CGAL新手问题: Which segments intersect?

一个时钟周期内的 C++ 字符串比较

swift - 归档时链接器失败

java - 在Java中使用C++模板框架

外部文件中的 C 函数无法正确返回?

c++ - Makefile:链接器错误