Seperation of Class into Files Problem

Hi all, I'm an absolute beginner to C++ and I am trying to separate my classes into different files i.e. "Nation.h" (declaration), "Nation.cpp" (implementation) and the main file "Mainfile.cpp".

This is my code right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//main file
#include <iostream>
#include "Nation.h"

using std::cout;
using std::cin;

int main (void)
{
	int stuffinput;
	cin>>stuffinput;
	Nation nation1(stuffinput); 
	cout<<nation1.show;
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
//Nation.h
#include <iostream>
#include <string>

class Nation
{
public:
	int show;
	Nation(int shownum);
	void func(void);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Nation.cpp
#include "Nation.h"
#include <iostream>

using std::cout;

Nation::Nation(int shownum)
{
	show=shownum;
}

void Nation::func(void)
{
	cout<<nation1.show*2;
}


When I try to run the programme, an error occurs and the compiler states that the "nation1" object in the "Nation.cpp" file is an undeclared identifier. This error doesn't occur if I combine the Nation class into the same file "Mainfile".

I've tried placing an #include "Mainfile.cpp" at the top of "Nation.cpp" but it was to no avail and there was an error still. To be honest I don't even know if that's allowed.

So my question is, how do I solve the error / let the object nation1 be recognised in the method "Nation.func()"?

This is in no way a homework question, I have just bought a book to learn C++ on my own an there's some areas which are not very well explained/elaborated so I decided to experiment with the codes on my own. Thank you.
Last edited on
Rewrite the member function

1
2
3
4
void Nation::func(void)
{
	cout<<nation1.show*2;
}

the following way

1
2
3
4
void Nation::func(void)
{
	cout<<show*2;
}
Hi vlad, thanks for the reply.

The problem is, I want the method func(void) to only produce an output for an object named nation1.

So if I have many objects nation1, nation2, nation3, etc of the Nation class, then I want all these objects to only produce the output nation1.show*2 instead of nation2.show*2 or nation3.show*2.
Each object has its own member show.
Sorry I wasn't too clear in my enquiry. This is the code if I didn't separate the classes into different files.

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

using std::cout;
using std::cin;
using namespace std;


class Nation
{
public:
	int show;
	Nation(int shownum);
	void func(void);
};

Nation nation1(5);
Nation nation2(7);

Nation::Nation(int shownum)
{
	show=shownum;
}

void Nation::func(void)
{
	cout<<nation1.show*2<<endl;
}


int main (void)
{
	cout<<nation1.show<<endl;
	nation1.func();
	nation2.func();
	return 0;
}


output:
1
2
3
5
10
10


Both nation1.func() and nation2.func() gives an output of 10 (=nation1.show*2) instead of 10(nation1.show*2) and 14(nation2.show*2).

I would like to know how to replicate this result if I separate the classes into different files.
Last edited on
What does the header "Nation.h" contain? Why is it included in your program?
Last edited on
Sorry, it doesn't contain anything. I removed it.
So if I have many objects nation1, nation2, nation3, etc of the Nation class, then I want all these objects to only produce the output nation1.show*2 instead of nation2.show*2 or nation3.show*2.


so, you can just write:
1
2
3
4
5
Nation nation1 (1);
Nation nation2 (3);
Nation nation3 (5);
//and just call:
nation1.func()
I already said you that you should remove nation1 from the function definition.

1
2
3
4
void Nation::func(void)
{
	cout<<nation1.show*2<<endl;
}
1
2
3
4
5
void Nation::func(void)
{
        //will show the show member of the object times 2
	cout<<show*2<<endl;
}


e.g.:
1
2
3
4
5
Nation nation1 (5);
Nation nation2 (9);

nation1.func(); //10
nation2.func(); //18 
So if I have many objects nation1, nation2, nation3, etc of the Nation class, then I want all these objects to only produce the output nation1.show*2
┬┐why is `nation1' so especial?

Regarding your code, you should realize that in the second snip `nation1' is global but in the first it's local to `main()'
Also, considering that you are not using the status of the object (but of a global one), there is no need to make it a method (just go with a function)
Topic archived. No new replies allowed.