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);
};
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.
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.
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 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)