Class

Apr 21, 2015 at 5:09pm
Can anyone help this example I try but it cannot run.
I dont know what is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include "box.h"

using namespace std;

int main()
{
	box<int> myBox;

	myBox.open();
	myBox.storeItem(50);
	myBox.removeItem();
	myBox.storeItem(150);

	return 0;
}


box.h

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
#include<iostream>

class box {
	public:
		void open();
		void removeItem();
		void storeItem(int x);
		void close();
		box();
	private:
		int item;
		bool boxOpen;
		bool empty;
};

using namespace std;

	box::box(){
		empty=true;
		boxOpen=false;
	}

	void box::open()
	{
		if (boxOpen)
				cout << "Box already open\n";
		else
		{
			boxOpen=true;
			cout << "Box is now open\n";
		}
	}


	void box::removeItem()
	{
		if (!boxOpen)
			cout << "Box is closed, please open first.\n";
		else if (!empty)
		{
			cout << "item " << item << "is removed from box";
			empty=true;
		}

		else
			cout << "No item to remove.\n";
	}

	void box::storeItem(int x)
	{
		if (!boxOpen)
			cout << "Box is closed, please open first.\n";
		else if (!empty)
			cout << "Cannot store new item, box is full.\n";
		else
		{
			item=x;
			cout << "Item " << x << " is stored.\n";
			empty = false;
		}
	}

	void box::close()
	{
		if (!boxOpen)
			cout << "Box is already closed\n";
		else
		{
			boxOpen=false;
			cout << "Box is now closed\n";
		}
	}

Last edited on Apr 21, 2015 at 5:23pm
Apr 21, 2015 at 5:20pm
You haven't defined box as a templated class. If you just use box myBox; it compiles and runs fine.
Last edited on Apr 21, 2015 at 5:22pm
Apr 21, 2015 at 5:23pm
What do your error messages say is wrong? Please post your error messages exactly as they appear in your development environment.

The first thing I see it that box is not a template class yet you're trying to treat it as a template class when you try to declare and instance.

Apr 21, 2015 at 5:27pm
How to defined box as template?
I using eclipse compiler.
I have put the header file for box.h but it did not work.
Can I know why we must using struct for private and public?
Apr 21, 2015 at 5:33pm
How to defined box as template?

Why do you want to define box as a template? Why not just declare box as a "normal" instance?

box myBox;


Edit: And your error messages should be telling where the problems were encountered, what are they?
Last edited on Apr 21, 2015 at 5:35pm
Apr 21, 2015 at 5:47pm
This error message.
'class box' has no member named 'removeItem' box.cpp /boxclass line 12 C/C++ Problem
'class box' has no member named 'open' box.cpp /boxclass line 10 C/C++ Problem
'class box' has no member named 'storeItem' box.cpp /boxclass line 11 C/C++ Problem
'class box' has no member named 'storeItem' box.cpp /boxclass line 13 C/C++ Problem

I just try how to make a program that use cpp file and the header file.
cpp file for the main program.
header file for definition.
Just to make the program easy to read.
Topic archived. No new replies allowed.