KarlSciberras
Programmer
Lately I have been experimenting with splitting classes in header files and cpp files. As far as I knew, the basic concept when splitting classes in header files and cpp files was the following:
header file --> declaration of class
cpp files --> implimentation / definition of class
However, the following did not
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
When the above code was compiled, i got the following errors:
error C2653: 'student' : is not a class or namespace name
error C2653: 'student' : is not a class or namespace name
error C2065: 'id' : undeclared identifier
After trying out some major possibilities of work arounds, the following worked:
-------------------------------------
--------------------------------------
---------------------------------------
-------------------------------------
The difference of the two examples of code described above is the following:
Example 1 - Student class declaration NOT included in student.cpp
Example 2 - Student class declaration was included in student.cpp
Would anyone please explain me, why a declaration of the student class is also required in the student.cpp so that the program compiles successfully? From what I learnt the header files should include the declaration and the cpp files should include the actual implementation...but apparently the decleration is also required in the cpp file,
is this true?
header file --> declaration of class
cpp files --> implimentation / definition of class
However, the following did not
-------------------------------------
Code:
//STUDENT.H
class student
{
public:
int id;
student::student();
};
Code:
//STUDENT.CPP
student::student()
{
student::id = 1;
}
Code:
//MAIN.CPP
#include <iostream>
#include <conio.h>
#include "student.h"
using namespace std;
void main()
{
student bob;
cout <<bob.id;
getch();
}
When the above code was compiled, i got the following errors:
error C2653: 'student' : is not a class or namespace name
error C2653: 'student' : is not a class or namespace name
error C2065: 'id' : undeclared identifier
After trying out some major possibilities of work arounds, the following worked:
-------------------------------------
Code:
//STUDENT.H
class student
{
public:
int id;
student::student();
};
Code:
//STUDENT.CPP
class student
{
public:
int id;
student::student();
};
student::student()
{
student::id = 1;
}
Code:
//MAIN.CPP
#include <iostream>
#include <conio.h>
#include "student.h"
using namespace std;
void main()
{
student bob;
cout <<bob.id;
getch();
}
The difference of the two examples of code described above is the following:
Example 1 - Student class declaration NOT included in student.cpp
Example 2 - Student class declaration was included in student.cpp
Would anyone please explain me, why a declaration of the student class is also required in the student.cpp so that the program compiles successfully? From what I learnt the header files should include the declaration and the cpp files should include the actual implementation...but apparently the decleration is also required in the cpp file,
is this true?