Forward declaration of class
Jun 21, 2014 at 7:25pm UTC
This is my global class:
1 2 3 4 5 6 7
#include "stdafx.h"
MyGlobalClass::MyGlobalClass(int argc, char * argv[]){
CLParser * CLPars( MyGlobalClass& );
FILE_ * File( MyGlobalClass& );
CLPars.ParseArgs(argc, argv);
}
The classes CLParser and file FILE_ was included before global class.
I need to have access to MyGlobalClass within these two classes. But the problem is that MyGlobalClass was not declared before. How to solve this problem?
I tried to access the global class as so:
1 2 3 4
class FILE_{
public :
void FILE_( MyGlobalClass& );
}
But MyGlobalClass is undeclared.
Last edited on Jun 22, 2014 at 12:11pm UTC
Jun 22, 2014 at 12:06pm UTC
I have updated the code:
Definitions.h:
1 2 3 4
#ifndef DESTINATION_CONST
#define DESTINATION_CONST
class MyGlobalClass;
#endif
CLParser.cpp:
1 2 3 4 5 6 7 8 9 10
#include "stdafx.h"
class CLParser{
private :
PGlobalInstance * globalInstance;
public :
CLParser( MyGlobalClass * globInst ){
PGlobalInstance = globInst;
}
void static ParseArgs(int argc, char * argv[]){}
}
File.cpp:
1 2 3 4 5
#include "stdafx.h"
FILE_::FILE_( MyGlobalClass * globInst){
PGlobalInstance = globInst;
}
main class global.h:
1 2 3 4 5
class MyGlobalClass{
private :
CLParser CLPars( MyGlobalClass );
FILE_ File( MyGlobalClass );
}
Now I get error on the last lines when using default constructor:
error C2146: syntax error : missing ';' before identifier 'CLPars'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
warnning C4183: 'CLPars': missing return type; assumed to be a member function returning 'int'
These are errors to the two lines:
CLParser CLPars ...
FILE_ File ...
What does mean these errors and warnings?
Last edited on Jun 22, 2014 at 12:44pm UTC
Jun 22, 2014 at 1:06pm UTC
Well, at the very least, your classes need some semicolons:
1 2 3 4
class MyClass
{
// ;)
};
And if you do have a circular dependency, yes, you could try using forward declaration.
Jun 22, 2014 at 1:26pm UTC
What's in stdafx.h?
Assuming it's empty or irrelevant, do this:
Definitions.h
1 2 3 4
#ifndef DESTINATION_CONST
#define DESTINATION_CONST
class MyGlobalClass;
#endif
CLParser.
h :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef CLPARSER_H
#define CLPARSER_H
class PGlobalInstance;
class MyGlobalClass;
class CLParser{
private :
PGlobalInstance * globalInstance;
public :
CLParser( MyGlobalClass * globInst ){
globalInstance = globInst;
}
void static ParseArgs(int argc, char * argv[]){}
};
#endif
File.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef FILE__H
#define FILE__H
class PGlobalInstance;
class MyGlobalClass;
class File_
{
private :
PGlobalInstance* globalInstance;
public :
FILE_( MyGlobalClass * globInst){
globalInstance = globInst;
}
};
#endif
main class global.h:
1 2 3 4 5 6 7
#include "File.h"
#include "CLParser.h"
class MyGlobalClass{
private :
CLParser CLPars( MyGlobalClass );
FILE_ File( MyGlobalClass );
};
Jun 22, 2014 at 2:54pm UTC
U R Good - You found the error (but I did it too). What I did wrong is that
PGlobalInstance should not be there at all. There should be
1 2 3 4
class CLParser{
private :
MyGlobalClass * PGlobalInstance;
}
Anyway, I still cannot make it working because of this error:
What's the problem here?
1 2 3 4 5 6 7 8 9
#include "stdafx.h"
FILE_::FILE_( MyGlobalClass * globInst){
PGlobalInstance = globInst;
}
int FILE_::IsDirectory(std::string path, char * workingPath){
PGlobalInstance->readDirectoryFiles = true ;
}
error C2227: left of '->readDirectoryFiles' must point to class/struct/union/generic type
How to solve this?
Edit:
Seems to be solved. The problem was readDirectoryFiles that was static method. I removed the word static and now it seems to work.
Last edited on Jun 22, 2014 at 3:26pm UTC
Topic archived. No new replies allowed.