complier error

Hi guys!

I am working on a small project which contains three files
1. MainTest.cpp
2. CashRegister.h
3. CashRegister.cpp

I have done everything that suppose to be done but still I am getting an error and can not execute the program.... can anyone points out what is wrong with my program?

Here is the code

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
// MainTest.cpp

#include <iomanip>
	using std::setw;
	using std::left;
#include <iostream>
	using std::cout;
	using std::endl;
	using std::ios;
#include "CashRegister.h"

const char ERROR[] = "Error! Registration failed."; 

int main()
{
	CashRegister cash("Register.txt", 5);

	cout << "This test should give five error messages below!" << endl;
	cout << "------------------------------------------------" << endl;

	if (!cash.registerItem( 3,"Tvål Sun", 18.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Äppelmos Bob", 14.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Coca Cola 2 lit.",  12.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Kroppkakor 4 st.", 18.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Mjölk mellan 1 lit", 6.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 2,"Skottkärran Emil",  389.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 2,"Räfsan Gunda", 110.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Socker bit ½ kg", 14.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 3,"Tandkräm Linnéa",  9.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 3,"Deodorant Axe", 37.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 4,"Aftonbladet", 8.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 4,"Mitt livs korsord",  39.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Svenssons äppelpaj", 30.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 4,"Skraplott Lyckans Ost", 25.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 5,"Räfflade dyckert 1000 st.",  89.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 5,"Hammaren Adam", 65.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Sörens Grova Limpa", 12.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Kaffe Gevalia Mörk",  29.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Glass Holland Eis", 32.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 2,"Myrgift Sudda Bort", 82.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 5,"Sandpapper blandat set",  32.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 3,"Munvatten Dentovit", 31.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 5,"Borrmaskinen Herbert", 310.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Makrillfiléer ½ kg",  38.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 6,"Golvlampan Hilda", 230.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 0,"Öl Pripps Blå 6-pack", 30.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 6,"Rakapparaten Orvar",  99.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 1,"Findus Köttfärssås 2,5 dl.", 16.00 ))
		cout << ERROR << endl;
	if (!cash.registerItem( 0,"Skruv 4x22 1000 st.", 80.50 ))
		cout << ERROR << endl;
	if (!cash.registerItem(-1,"Blå Castello  100 gr.",  19.50 ))
		cout << ERROR << endl << endl;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << setw(30) << left << "The total for this sail was:" << cash.batchTotal() << " SEK" << endl;
	cout << setw(30) << left << "Expected result should be: " << "1473.00 SEK" << endl << endl;

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//CashRegister.cpp
class CashRegister{
public:
	CashRegister(char* fileName, int nrOfCategories);// The file contains
	~CashRegister(); // registered items and balances

	bool registerItem(int category, char* articleName, double amount);
	double batchTotal(){
		double total = _batchTotal;
		_batchTotal = 0;	// batchTotal() is expected to reset _batchTotal;
		return total;
	}
private:
	ofstream _file; // Here will registered items be saved
	int _nrOfCategories; // products are organized in categories
	double* _categorySums; // The vector will contain respective sum of each category sold during the day
	double _batchTotal; // sold since the last summation
};


1
2
3
4
5
6
7
8
9
// CashRegister.h
#ifndef CASHREGISTER_H
#define CASHREGISTER_H

	CashRegister(char* fileName, int nrOfCategories);
	bool registerItem(int category, char* articleName, double amount);
    double batchTotal();

#endif 


any kind of help will be appreciated... thanks in advance

Sincere Regards
What error? is it compile time or runtime? What error message is?
Hi guys!

Thanks for your time and reply. I have made the changes you have suggested and moved the class declaration and definition to .h file

and now the error I am getting is error: 'ofstream' does not name a type

the line it is point out is:
ofstream _file;
1) You could forgot to #include <fstream>
2) You might want to use qualified name std::ofstream
the error I am getting is

undefined reference to 'CashRegister::CashRegister(char*, int)'
undefined reference to 'CashRegister::registerItem(int, char*, double)'
Your .h file does not contain class declaration. It only have freestanding functions.
so how to fix it? do i need to write class declaration in the .cpp file? or in the headerfile?
Class declaration in header file.
Definition of class functions in cpp file.
Include .h file where needed
Make sure that cpp file is compiled and linked.
Topic archived. No new replies allowed.