header files

Hi all! This is my first time using a header file and source code together. I am getting a external symbol error that I think might be due to using this method for the first time? Could someone help me find the bug?

The header file is first, them the source code. I have not written the driver code yet, I wanted to handle this error first.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #pragma once

#ifndef inventory_h
#define inventory_h
#include <iostream>
using namespace::std;

class inventory
{
public:
	inventory();
	inventory(int id, int store, int qt);

	void setId(int id);
	void setStoreNr(int store);
	void setQuantity(int qt);

	int getId(void)			const;
	int getStoreNr(void)	const;
	int getQuantity(void)	const;

private:
	int itemId;
	int storeNr;
	int quantity;

};
#endif

//here is the source code below

#include "inventory.h"

inventory::inventory()
{
	itemId = 0;
	storeNr = 0;
	quantity = 0;
}

inventory::inventory(int id, int store, int qt)
{
	itemId = id;
	storeNr = store;
	quantity = qt;
}


void inventory::setId(int id)
{
	itemId = id;
}

void inventory::setStoreNr(int store)
{
	storeNr = store;
}

void inventory::setQuantity(int qt)
{
	quantity = qt;
}

int inventory::getId(void) const
{
	return itemId;
}

int inventory::getStoreNr(void)	const
{
	return storeNr;
}

int inventory::getQuantity(void)	const
{
	return quantity;
}


thank you!!
closed account (48T7M4Gy)
I think your best bet is to forge ahead and make a simple couple of lines test program (int main() etc) and make it all one file as you've done above. Just edit and change your post accordingly. That way we can run it and see the errors first hand. Make sure the amended file can be run, we don't have to add lines etc.
I've tried this, and the first two files are fine when they are put together. However, now that I've added the driver, I cannot access the class members.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once

#ifndef inventory_h
#define inventory_h
#include <iostream>
#include <fstream>
#include <iomanip>

const int MAX_AR_SZ = 25;		// Max array size
const int MAX_FILENAME_LEN = 40;// Max array size for fileName string


using namespace::std;

class inventory
{
public:
	inventory();
	inventory(int id, int store, int qt);

	void setId(int id);
	void setStoreNr(int store);
	void setQuantity(int qt);

	int getId(void)			const;
	int getStoreNr(void)	const;
	int getQuantity(void)	const;

private:
	int itemId;
	int storeNr;
	int quantity;

};
#endif

inventory::inventory()
{
	itemId = 0;
	storeNr = 0;
	quantity = 0;
}

inventory::inventory(int id, int store, int qt)
{
	itemId = id;
	storeNr = store;
	quantity = qt;
}


void inventory::setId(int id)
{
	itemId = id;
}

void inventory::setStoreNr(int store)
{
	storeNr = store;
}

void inventory::setQuantity(int qt)
{
	quantity = qt;
}

int inventory::getId(void) const
{
	return itemId;
}

int inventory::getStoreNr(void)	const
{
	return storeNr;
}

int inventory::getQuantity(void)	const
{
	return quantity;
}

bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells);
void printArrays(ostream & con, inventory invList[], int count);
bool extractData(const char newFileName[], int requestId, int baseQty,
	inventory invList[], int count, int & newcount);
int main()
{
	inventory invLs[MAX_AR_SZ];
	int cellsLoaded, baseQty, written;
	long reqId;
	char fileName[MAX_FILENAME_LEN];

	cout << "\tProject 4 -- Ms. Chen\n" << endl;
	cout << "Filename:";
	cin >> fileName;
	if (!loadArrays(fileName, invLs, cellsLoaded, MAX_AR_SZ))
	{
		return 0;
	}
	printArrays(cout, invLs, cellsLoaded);
	cout << "Filename:";
	cin >> fileName;
	cout << "Product :";
	cin >> reqId;
	cout << "Quantity:";
	cin >> baseQty;
	if (!extractData(fileName, reqId, baseQty, invLs, cellsLoaded, written))
		return 0;
	cout << endl; return 0;	// Closing PAKTC...
}
bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells)
{
	count = 0;
	ifstream In;
	In.open("C:\\Users\\Sophie\\Downloads\\Project3Records.txt");
	if (!In)
	{
		cout << "Error 1: Cannot find file\n\t" << fileName << endl;
		return false;
	}
	while (count < MAX_AR_SZ && In >> invList[count].itemId
		>> invList[count].store >> invList[count].qty)
	{
		count++;
		if (count >= maxCells && In)
		{
			cout << "Error 3: " << fileName << " contains more data than array can hold\n";
			return false;
		}
	}
	return true;
}
void printArrays(ostream & con, inventory invList[], int count)
{
	cout << setw(12) << "Product ID" << setw(6) << "Store"
		<< setw(5) << "Qty" << endl;
	for (int i = 0; i < count; i++)
	{
		con << setw(12) << invList[i].id << setw(6) << invList[i].store
			<< setw(5) << invList[i].qty << endl;
	}
}
bool extractData(const char newFileName[], int requestId, int baseQty,
	inventory invList[], int count, int & newcount)
{
	ofstream Out;
	Out.open(newFileName);
	if (!Out)
	{
		cout << "Error 2: Could not extract to\n\t" << newFileName << endl;
		return false;
	}
	newcount = 0;
	for (int i = 0; i < count; i++)
		if (invList[i].id == requestId && invList[i].qty < baseQty)
		{
			Out << invList[i].id << " " << invList[i].store
				<< " " << invList[i].qty << " " << endl;
			++newcount;
		}
	return true;
}


Last edited on
closed account (48T7M4Gy)
OK but give me time to have a look.
closed account (48T7M4Gy)
So where is the bug?? What is going wrong. I replaced your main with a simple driver and it appears to work OK.
Thank you! That helped. I am still having an error regarding function calls. The program says that I have overloaded loadArrays and extractData in "more than one instance." It looks to me like it is only defined once and calling once?

Also, an error that In doesn't accept the operands >> in the loadArrays function.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

#pragma once

#ifndef inventory_h
#define inventory_h
#include <iostream>
#include <fstream>
#include <iomanip>

const int MAX_AR_SZ = 25;		// Max array size
const int MAX_FILENAME_LEN = 40;// Max array size for fileName string

bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells);
void printArrays(ostream & where, inventory invList[], int count);
bool extractData(const char newFileName[], int requestId, int baseQty,
	inventory invList[], int count, int & newcount);

using namespace::std;

class inventory
{
public:
	inventory();
	inventory(int id, int store, int qt);

	void setId(int id);
	void setStoreNr(int store);
	void setQuantity(int qt);

	int getId(void)			const;
	int getStoreNr(void)	const;
	int getQuantity(void)	const;

private:
	int itemId;
	int storeNr;
	int quantity;

};
#endif

inventory::inventory()
{
	itemId = 0;
	storeNr = 0;
	quantity = 0;
}

inventory::inventory(int id, int store, int qt)
{
	itemId = id;
	storeNr = store;
	quantity = qt;
}


void inventory::setId(int id)
{
	itemId = id;
}

void inventory::setStoreNr(int store)
{
	storeNr = store;
}

void inventory::setQuantity(int qt)
{
	quantity = qt;
}

int inventory::getId(void) const
{
	return itemId;
}

int inventory::getStoreNr(void)	const
{
	return storeNr;
}

int inventory::getQuantity(void)	const
{
	return quantity;
}


int main()
{
	inventory invLs[MAX_AR_SZ];
	int cellsLoaded, baseQty, written;
	long reqId;
	char fileName[MAX_FILENAME_LEN];

	if (!loadArrays(fileName, invLs, cellsLoaded, MAX_AR_SZ))
	{
		return 0;
	}
	printArrays(cout, invLs, cellsLoaded);

	cout << "Product :";
	cin >> reqId;
	cout << "Quantity:";
	cin >> baseQty;

	extractData(fileName, reqId, baseQty, invLs, cellsLoaded, written))
	
}
bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells)
{
	count = 0;
	ifstream In;
	In.open("C:\\Users\\Sophie\\Downloads\\Project3Records.txt");
	if (!In)
	{
		cout << "Error 1: Cannot find file\n\t" << fileName << endl;
		return false;
	}
	while (count < MAX_AR_SZ && In >> invList[count].getId()
		>> invList[count].getStoreNr() >> invList[count].getQuantity())
	{
		count++;
		if (count >= maxCells && In)
		{
			cout << "Error 3: " << fileName << " contains more data than array can hold\n";
			return false;
		}
	}
	return true;
}
void printArrays(ostream & where, inventory invList[], int count)
{
	cout << setw(12) << "Product ID" << setw(6) << "Store"
		<< setw(5) << "Qty" << endl;
	for (int i = 0; i < count; i++)
	{
		where << setw(12) << invList[i].getId() << setw(6) << invList[i].getStoreNr()
			<< setw(5) << invList[i].getQuantity() << endl;
	}
}
bool extractData(const char newFileName[], int requestId, int baseQty,
	inventory invList[], int count, int & newcount)
{
	ofstream Out;
	Out.open("C:\\Users\\Sophie\\Downloads\\Project3.txt");
	if (!Out)
	{
		cout << "Could not open file" << endl;
		return false;
	}
	newcount = 0;
	for (int i = 0; i < count; i++)
		if (invList[i].getId() == requestId && invList[i].getQuantity() < baseQty)
		{
			Out << invList[i].getId() << " " << invList[i].getStoreNr()
				<< " " << invList[i].getQuantity() << " " << endl;
			++newcount;
		}
	return true;
}

I am getting compiler errors. What is a simple drive?
closed account (48T7M4Gy)
A simple driver for the class is:

But your problem here requires commenting out the lines you have in your main() because the class is OK.

The main() I had was:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    inventory invls[MAX_AR_SZ];
    
    for (int i = 0; i < MAX_AR_SZ; i++ )
    {
        invls[i].setId(i + 15);
        invls[i].setStoreNr(i*100 + 1000);
        invls[i].setQuantity(10000 * i);
    }
    
    for (int i = 0; i < MAX_AR_SZ; i++ )
        cout << invls[i].getId() << '\t' << invls[i].getStoreNr() << '-' << invls[i].getQuantity() << endl;
    
    return 0;
}


I'll now try yours :)
closed account (48T7M4Gy)
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
bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells)
{
    count = 0;
    ifstream In;
    In.open("C:\\Users\\Sophie\\Downloads\\Project3Records.txt");
    if (!In)
    {
        cout << "Error 1: Cannot find file\n\t" << fileName << endl;
        return false;
    }
    
    int temp1, temp2, temp3; // <--
    
    while (count < MAX_AR_SZ && In >> temp1  >> temp2 >> temp3) //<--
    {
        invList[count].setId(temp1); // <--
        invList[count].setStoreNr(temp2); // <-- etc
        
        count++;
        if (count >= maxCells && In)
        {
            cout << "Error 3: " << fileName << " contains more data than array can hold\n";
            return false;
        }
    }
    return true;
}


try loadarrays with this after you make complete changes I indicate with arrows
Last edited on
closed account (48T7M4Gy)
1
2
bool extractData(const char newFileName[], long requestId, int baseQty,
	inventory invList[], int count, int & newcount)


Change the int to long everywhere. (2 places)
Last edited on
closed account (48T7M4Gy)
1
2
invList[count].setId(temp1); // <--
invList[count].setStoreNr(temp2); // <-- etc 
closed account (48T7M4Gy)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <fstream>
#include <iomanip>

const int MAX_AR_SZ = 25;		// Max array size
const int MAX_FILENAME_LEN = 40;// Max array size for fileName string

using namespace::std;

class inventory
{
public:
    inventory();
    inventory(int id, int store, int qt);
    
    void setId(int id);
    void setStoreNr(int store);
    void setQuantity(int qt);
    
    int getId(void)			const;
    int getStoreNr(void)	const;
    int getQuantity(void)	const;
    
private:
    int itemId;
    int storeNr;
    int quantity;
    
};

inventory::inventory()
{
    itemId = 0;
    storeNr = 0;
    quantity = 0;
}

inventory::inventory(int id, int store, int qt)
{
    itemId = id;
    storeNr = store;
    quantity = qt;
}

void inventory::setId(int id){itemId = id;}
void inventory::setStoreNr(int store){ storeNr = store;}
void inventory::setQuantity(int qt){quantity = qt;}
int inventory::getId(void) const{return itemId;}
int inventory::getStoreNr(void)	const{return storeNr;}
int inventory::getQuantity(void) const{return quantity;}

bool loadArrays(const char[], inventory[], int& , int);
void printArrays(ostream&, inventory[], int);
bool extractData(const char[], long, int,inventory[], int, int &);

int main()
{
    inventory invLs[MAX_AR_SZ];
    int cellsLoaded, baseQty, written;
    long reqId;
    char fileName[MAX_FILENAME_LEN];
    
    if (!loadArrays(fileName, invLs, cellsLoaded, MAX_AR_SZ))
    {
        return 0;
    }
    
    printArrays(cout, invLs, cellsLoaded);
    
    cout << "Product :";
    cin >> reqId;
    cout << "Quantity:";
    cin >> baseQty;
    
    extractData(fileName, reqId, baseQty, invLs, cellsLoaded, written);
    
    return 0;
}

bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells)
{
    count = 0;
    ifstream In;
    In.open("C:\\Users\\Sophie\\Downloads\\Project3Records.txt");
    if (!In)
    {
        cout << "Error 1: Cannot find file\n\t" << fileName << endl;
        return false;
    }
    
    int temp1, temp2, temp3; // <--
    
    while (count < MAX_AR_SZ && In >> temp1  >> temp2 >> temp3) //<--
    {
        invList[count].setId(temp1); // <--
        invList[count].setStoreNr(temp2); // <--
        invList[count].setQuantity(temp3); // <--
        
        count++;
        if (count >= maxCells && In)
        {
            cout << "Error 3: " << fileName << " contains more data than array can hold\n";
            return false;
        }
    }
    return true;
}

void printArrays(ostream & where, inventory invList[], int count)
{
    cout << setw(12) << "Product ID" << setw(6) << "Store"
    << setw(5) << "Qty" << endl;
    for (int i = 0; i < count; i++)
    {
        where << setw(12) << invList[i].getId() << setw(6) << invList[i].getStoreNr()
        << setw(5) << invList[i].getQuantity() << endl;
    }
}

bool extractData(const char newFileName[], long requestId, int baseQty,inventory invList[], int count, int & newcount)
{
    ofstream Out;
    Out.open("C:\\Users\\Sophie\\Downloads\\Project3.txt");
    if (!Out)
    {
        cout << "Could not open file" << endl;
        return false;
    }
    newcount = 0;
    for (int i = 0; i < count; i++)
        if (invList[i].getId() == requestId && invList[i].getQuantity() < baseQty)
        {
            Out << invList[i].getId() << " " << invList[i].getStoreNr()
            << " " << invList[i].getQuantity() << " " << endl;
            ++newcount;
        }
    return true;
}
Last edited on
Topic archived. No new replies allowed.