Can't pass a string to a constructor

Here are my 3 files:

GroceryItem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Specification File hw3.h

#ifndef GroceryItem_h
#define GroceryItem_h

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//using std::string;

class GroceryItem {
public:
                //GroceryItem();
        GroceryItem(int PLU,string name,int type, double price,double
level);
};

#endif


GroceryItem.cpp
1
2
3
4
5
6
7
8
9
10
#include <string>
using namespace std;

#include "GroceryItem.h"

//void GroceryItem::GroceryItem() {}
void GroceryItem::GroceryItem(int PLU, string  name,int type,double price,
double level) {

}


hw3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Implementation file hw3.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace 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

Any help would be greatly appreciated!

Thanks,
Travis
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.
Last edited on
Topic archived. No new replies allowed.