//Implementation file hw3.cpp
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
#include "GroceryItem.h"
#include <cstdlib> //for exit function
int main() {
//Open file
ifstream myfile;
myfile.open("inventory.txt");
if(!myfile) {//file couldn't be opened
cerr << "Error: file couldn't be opened" << endl;
exit(1);
}
//Initialize Grocery store
GroceryItem *storefront[100];
int i=0;
int PLU,type;
string name;
double price,level;
while(!myfile.eof()) {//keep reading until end-of-file
//read in lines
myfile >> PLU;
myfile>>name;
// getline(myfile,name);
myfile>>type;
myfile>>price;
myfile>>level;
storefront[i] = new GroceryItem(PLU,name,type,price,level);
i++;
}
myfile.close();
char ans;
int product;
do
{
cout<<"Enter product code: ";
cin >> product;
cout<<"Do you want to purchase something else (Y/N)? ";
cin>>ans;
}
while((ans=='Y'||ans=='y'));
return 0;
}
I get the following error when compiling:
{localhost:~}g++ hw3.cpp
/usr/tmp/cc9hetbt.o: In function `main':
hw3.cpp:(.text+0x17d): undefined reference to `GroceryItem::GroceryItem(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
In your implementation you are defining a new function, not the constructor. Drop the 'void' as a return type as constructors do not have a return type.