【问题标题】:VS2010 fatal error LNK1120: 1 unresolved externalsVS2010 致命错误 LNK1120: 1 未解决的外部
【发布时间】:2012-06-03 20:57:15
【问题描述】:

我已经为此工作了一段时间,但我似乎无法弄清楚。我无法编译代码,因为它不断抛出以下错误。基本上我试图做的是创建一个向量,其中包含使用指针来自基类和派生类的对象。

Final Assignment.obj:错误 LNK2019:函数“public: __thiscall SaleProduct::SaleProduct(char,类 std::basic_string,类 std::allocator >,int,double,double)" (??0SaleProduct@@QAE@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@ 2@@std@@HNN@Z)
致命错误 LNK1120:1 个未解决的外部问题

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;

class Product
{


public:

Product();
Product(char iT,string des,int inv,double p,double dis)
{
invType = iT;
description = des;
inventory = inv;
price = p;
discount = dis;
}

char getInvType(){
return invType;
}

protected:
char invType ;
string description;
int inventory;
double price;
double discount;
};

class SaleProduct:public Product {

public:



//SaleProduct();
SaleProduct(char iT,string des,int inv,double p,double dis){


}


};



int main()
{
int choice = 0;
// SaleProduct* SaleProductOB;
// Product *productPoint = &ProductOB;
 vector<Product*> inventoryVec;

  char invType;
  string description;
  int inventory;
  double price;
  double discount = 0;




ifstream inFile("Inventory.txt");

if (inFile.is_open()){

while (inFile >> invType >> description >> inventory >> price >>discount){

      if (invType == 'S'){

           inventoryVec.push_back(new SaleProduct(invType,description,inventory,price,discount)  );
     }else{
    //inventoryVec.push_back(new Product(invType,description,inventory,price,discount)  );

      }

}
}else{
cout << "File is not ready!";
}



cout << inventoryVec.size() << endl;


while (choice != 3) {
        cout << "Please enter your choice:" << endl;
        cout << "1. Print available items"  << endl;
        cout << "2. Add item to cart"       << endl;
        cout << "3. Print receipt and quit" << endl << endl;

        cin >> choice;
 }








//system("PAUSE");
return 0;
}

【问题讨论】:

  • 如果您真的希望 Product 默认构造函数为空,请设置为空; Product() {}.
  • 先生,您真是个天才!如果我已经指定了构造函数,你知道为什么还需要默认构造函数吗?
  • 你不需要一个,我认为你正在尝试调用基类的非默认构造函数,但没有语法来做到这一点。有关如何执行此操作,请参阅下面的 Mark 回答,您可以删除默认构造函数。

标签: c++ visual-c++


【解决方案1】:

您可能需要指定正确的构造函数(因为它正在尝试使用没有实际定义的默认构造函数):

SaleProduct(char iT,string des,int inv,double p,double dis) : 
   Product(iT, des, inv, p, dis ){

【讨论】:

  • 我想我明白了。那么,由于 SaleProduct 类派生自 Product 类,因此 SaleProduct 构造函数会尝试使用 Product 构造函数吗?
  • @AndresRubalcava 是的,默认会使用默认(无参数)构造函数,以上是使用非默认构造基类的一种方式。
  • @Mark Wilkins && Joachim Isaksson 非常感谢你们的帮助。我会多读一些关于构造函数的内容。
猜你喜欢
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多