C++ Class program problems

I am writing a program with a class and I don't understand what i am doing wrong, where am i messing up my 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


#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Presidents
{
private: 
	int _number;
	string _name;
	string _quote;
public:
	Presidents ();
	~Presidents();
	void setNumber(int number);
	void setName(string name);
	void setQuote(string quote);
	void displayPresidents()const;
};

#include"Presidents.h"
Presidents :: Presidents ()
{
	_number = 0;
}
Presidents :: ~Presidents(){}
void Presidents :: setNumber(int number)
{
	_number = number;
}

void Presidents :: setName(string name)
{
	_name = name;
}
void Presidents :: setQuote(string quote)
{
	_quote = quote;
}
void Presidents ::displayPresidents() const
{
	int count = 0;
	cout << "The presidents are: \n";
	for (int i = 0; i < count; i ++)
	{
		cout << _name << ", "<< _number << " president, said: \"" << _quote << ".\"\n";
	}
}
#include "Presidents.cpp"

int main()
{
	Presidents president;
	int number;
	string name;
	string quote; 
	int count = 0;
	for (int i = 0; i < 3; i ++)
	{
		cout << "Enter the first president's number: " ;
		cin >> number;
		cin.ignore();
		cout << "\nEnter his name: ";
		getline(cin, name);
		cout << "\nEnter the his quote: ";
		getline(cin, quote);
		president.setNumber(number);
		president.setName(name);
		president.setQuote(quote);
		count ++;
	}

	return 0;
}
First,
1
2
3
4
5
6
int count = 0;
for (int i = 0; i < count; i ++)
// is same as
for (int i = 0; i < 0; i ++)
// is same as


More importantly, you have one Presidents that has one number, one name, and one quote.

You seem to want a list. Either a list of "president" objects, or the Presidents should have lists of numbers, names, and quotes.
Code looks fine.. where u r facing the problem.. what the error it is throwing compilation or run-time
Last edited on

Not sure why you are including president.h and president.cpp, those are not needed for your code.

I would suggest looking over the classes tutorial again to get a better understanding: http://www.cplusplus.com/doc/tutorial/classes/

In your main you are only creating 1 single object (or instance) of the Presidents class, one object can only store 1 value in _number, _name, _quote and therefore displayPresidents() just wont work.

On the note of functions (forget its in the class for now), how would displayPresidents() know how many times to loop to show contents if you havent passed count to it? - you have assigned count inside the function to 0 and therefore it would never loop.

Anyway, I have tidied up your code a little and hopefully you will see what I mean.

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

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Presidents
{
private:
	int _number;
	string _name;
	string _quote;
public:
	Presidents() { _number = 0; }
	~Presidents() {}
	// set the private variables
	void setNumber(int number) { _number = number; }
	void setName(string name) { _name = name; }
	void setQuote(string quote) { _quote = quote; }
	// return the private variables ( because they are private
	// then outside of the class object they cant be seen or
	// used so we need to create functions to pass them safely )
	int getNumber() { return _number; }
	string getName() { return _name; }
	string getQuote() { return _quote; }
};


int main()
{
	// we are assuming your storing 3 presidents,
	// if its unknown at runtime you need to look
	// at using vectors

	// http://www.cplusplus.com/reference/vector/vector/

	Presidents president[3];

	int number;
	string name;
	string quote;

	for (int i = 0; i < 3; i++)
	{
		cout << "Enter president no."<<i+1<<"'s number: ";
		cin >> number;
		cin.ignore();
		cout << "\nEnter his name: ";
		getline(cin, name);
		cout << "\nEnter the his quote: ";
		getline(cin, quote);
		// Store in class object at index i
		president[i].setNumber(number);
		president[i].setName(name);
		president[i].setQuote(quote);

	}

	// lets display the information in our three class objects.
	for (int i = 0; i < 3; i++)
	{
		cout << "Name: " << president[i].getName() << ", No. " << president[i].getNumber() << ", Quote: " << president[i].getQuote() << endl;
	}

	return 0;
}

when i build these error messages come up!

Presidents.obj : error LNK2005: "public: __thiscall Presidents::Presidents(void)" (??0Presidents@@QAE@XZ) already defined in main.obj
Presidents.obj : error LNK2005: "public: __thiscall Presidents::~Presidents(void)" (??1Presidents@@QAE@XZ) already defined in main.obj
Presidents.obj : error LNK2005: "public: void __thiscall Presidents::setNumber(int)" (?setNumber@Presidents@@QAEXH@Z) already defined in main.obj
Presidents.obj : error LNK2005: "public: void __thiscall Presidents::setName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setName@Presidents@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in main.obj
Presidents.obj : error LNK2005: "public: void __thiscall Presidents::setQuote(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setQuote@Presidents@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in main.obj
Presidents.obj : error LNK2005: "public: void __thiscall Presidents::displayPresidents(void)const " (?displayPresidents@Presidents@@QBEXXZ) already defined in main.obj
c:\users\suha\documents\visual studio 2010\Projects\Presidents Class\Debug\Presidents Class.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I need to to have both the presidents.h and the presidents.cpp. I cant combine them.
The int getNumber string getName and string getQuote are supposed to be void functions, not value returning functions
Create a header file
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
class Presidents
{
private: 
	int _number;
	string _name;
	string _quote;
public:
	Presidents ();
	~Presidents();
	void setNumber(int number);
	void setName(string name);
	void setQuote(string quote);
	void displayPresidents()const;
};
Presidents :: Presidents ()
{
	_number = 0;
}
Presidents :: ~Presidents(){}
void Presidents :: setNumber(int number)
{
	_number = number;
}

void Presidents :: setName(string name)
{
	_name = name;
}
void Presidents :: setQuote(string quote)
{
	_quote = quote;
}
void Presidents ::displayPresidents() const
{
	int count = 0;
	cout << "The presidents are: \n";
	for (int i = 0; i < count; i ++)
	{
		cout << _name << ", "<< _number << " president, said: \"" << _quote << ".\"\n";
	}
}


Then create a cpp file calling the above header file
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
#include<iostream>
#include<fstream>
#include<string>
#include"Presidents.h"

using namespace std;

int main()
{
	Presidents president;
	int number;
	string name;
	string quote; 
	int count = 0;
	for (int i = 0; i < 3; i ++)
	{
		cout << "Enter the first president's number: " ;
		cin >> number;
		cin.ignore();
		cout << "\nEnter his name: ";
		getline(cin, name);
		cout << "\nEnter the his quote: ";
		getline(cin, quote);
		president.setNumber(number);
		president.setName(name);
		president.setQuote(quote);
		count ++;
	}
	return 0;
}
I know how to do the above, If i wanted to I could do that, but I cant combine presidents.h and presidents.cpp. I need to have all three of them separated like in the code i originally posted :(
Just create Presidents.hPresidents.cpp leave it empty and run the program it should work now.
The int getNumber string getName and string getQuote are supposed to be void functions, not value returning functions


They cant be void, you have declared private variables inside your class and unless its the actual object of the class accessing them then you cant see or use them inside your main, i.e. you need a way of returning those private variables back to whoever needs to use them.



Well if your separating then it goes something like this (using my example code above)...

Your header file...

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

// presidents.h


class Presidents
{
private:
	int _number;
	string _name;
	string _quote;
public:
	Presidents() { _number = 0; }
	~Presidents() {}
	// set the private variables
	void setNumber(int number) { _number = number; }
	void setName(string name) { _name = name; }
	void setQuote(string quote) { _quote = quote; }
	// return the private variables ( because they are private
	// then outside of the class object they cant be seen or
	// used so we need to create functions to pass them safely )
	int getNumber() { return _number; }
	string getName() { return _name; }
	string getQuote() { return _quote; }
};


Your CPP file...

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

#include<iostream>
#include<fstream>
#include<string>
using namespace std;


#include "presidents.h"		// header with class declaration


int main()
{
	// we are assuming your storing 3 presidents,
	// if its unknown at runtime you need to look
	// at using vectors

	// http://www.cplusplus.com/reference/vector/vector/

	Presidents president[3];

	int number;
	string name;
	string quote;

	for (int i = 0; i < 3; i++)
	{
		cout << "Enter president no." << i + 1 << "'s number: ";
		cin >> number;
		cin.ignore();
		cout << "\nEnter his name: ";
		getline(cin, name);
		cout << "\nEnter the his quote: ";
		getline(cin, quote);
		// Store in class object at index i
		president[i].setNumber(number);
		president[i].setName(name);
		president[i].setQuote(quote);

	}

	// lets display the information in our three class objects.
	for (int i = 0; i < 3; i++)
	{
		cout << "Name: " << president[i].getName() << ", No. " << president[i].getNumber() << ", Quote: " << president[i].getQuote() << endl;
	}

	return 0;
}



Failing that then I've not got a clue what you mean - if that's the case then I would suggest posting your actual assignment details.
Topic archived. No new replies allowed.