我在这方面已经研究了一段时间了,但我似乎还是搞不清楚。我无法编译代码,因为它一直抛出以下错误。基本上,我试图做的是创建一个向量,其中包含使用指针的基类和派生类的对象。
Final Assignment.obj:错误LNK2019:在函数“public:__thiscall SaleProduct::SaleProduct(char,class std::Basic_String,class std::Allocator>,int,double,double)”(??0SaleProduct@@qae@dv?$Basic_String@du?$CHAR_Traits@d@std@@v?$Allocator@d@2@@std@@hnn@z)中引用了未解析的外部符号“public:__thiscall Product::Product(
#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;
}
您可能需要指定正确的构造函数(按原样,它正在尝试使用缺省构造函数,而缺省构造函数没有实际定义):
SaleProduct(char iT,string des,int inv,double p,double dis) :
Product(iT, des, inv, p, dis ){