Hello! I'm having trouble with my program. Here's my problem:
I have a program with multiple source files, and I try to make a class in class.cpp, but when I try to use it in main.cpp it gives me an error.
Here's a dummyed down version:
1 2 3 4 5 6 7 8 9 10 11
//main.cpp
#include <iostream>
#include "main.h"
usingnamespace std;
int main(){
CLASS x;
x.i=5;
cout << x.i;
}
//class.h
#ifndef CLASS_H
#define CLASS_H
class CLASS;
#endif
1 2 3 4 5
//class.cpp
#include "class.h"
class CLASS{
int i;
};
And the error:
aggregate `CLASS x' has incomplete type and cannot be defined
This can be fixed easily by adding the class definition from class.cpp into main.cpp. However my entire point of making multiple files was to organize myself. Is there anyway to keep the entire definition in one file?
You must define the class in the header file. I don't think classes translate into symbols, so all the compiler is seeing (when in main.cpp) is the forward declaration of class CLASS, and not the definition.