I have segmentation fault problem with my multiple class files project.
Without making empty classes and then pointers I get “XXX does not name a type” error.
How can I fix this or do this another way? I can not make class AA in class A.
main.cpp
#include "A.h"
A a;
int main() {
while(true) {
}
return 1;
}
A.h
#ifndef A_H_
#define A_H_
class AA;
#include "AA.h"
class A {
public:
A();
virtual ~A();
AA *aa;
void run();
};
#endif /* A_H_ */
A.cpp
#include "A.h"
A::A() {
// TODO Auto-generated constructor stub
}
A::~A() {
// TODO Auto-generated destructor stub
}
void A::run() {
aa->run();
}
AA.h
#ifndef AA_H_
#define AA_H_
#include <iostream>
class AA {
public:
AA();
virtual ~AA();
void run();
};
#endif /* AA_H_ */
AA.cpp
#include "AA.h"
AA::AA() {
// TODO Auto-generated constructor stub
}
AA::~AA() {
// TODO Auto-generated destructor stub
}
void AA::run() {
std::cout << "1";
}
All you need is an include of AA.h in A.cpp.
This is a quite common scenario. Whe your interface needs one thing and your implementation needs other stuff. Not all includes should to be done in the .h file. Also this should not produce a segfault, it should be not compiling.
On the same note, you are including iostream in the .h file, you are using it in the .cpp file, why not include it there instead?
REREAD: You are not constructing your aa object.
issue a
or similar in your constructor. Currently you are trying to access an object that has not been constructed.