How to refer to class which was not yet declared?
This is my global class:
Code:
#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_ were 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:
Code:
class FILE_{
public:
void FILE_( MyGlobalClass& );
}
But MyGlobalClass is undeclared.
Re: How to refer to class which was not yet declared?
You need to forward declare the classes required. But note that for a forward declared class you can only access the elements via pointers.
See http://stackoverflow.com/questions/9...rd-declaration
http://stackoverflow.com/questions/5...rd-declaration
Before the definiton of CLParser class, put
Code:
class MyGlobalClass;
but note the comment re access to the members of MyGlobalClass.
Re: How to refer to class which was not yet declared?
I already found it searching web. But I still have some nasty errors :-) Saying things like that I am missing ';' when I try to create new instance of MyGlobalClass and _FILE. I cannot find out where the ; could be missing. global.h:
Code:
class MyGlobalClass{
private:
CLParser CLPars;
FILE_ File;
};
Any idea why this could happen?
Re: How to refer to class which was not yet declared?
What's the problem here?
Code:
#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?
Re: How to refer to class which was not yet declared?
Quote:
Originally Posted by
CrazyBoy
I already found it searching web. But I still have some nasty errors :-) Saying things like that I am missing ';' when I try to create new instance of MyGlobalClass and _FILE. I cannot find out where the ; could be missing. global.h:
Code:
class MyGlobalClass{
private:
CLParser CLPars;
FILE_ File;
};
Any idea why this could happen?
Often because the compiler isn't recognising CLParser or FILE_ as a type/class. Note that if CLParser or FILE_ is forward declared you can't use it in statements like this as the class isn't fully defined at this point.
Re: How to refer to class which was not yet declared?
Quote:
Originally Posted by
CrazyBoy
What's the problem here?
Code:
#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?
Where is PGlobalInstance defined?
Re: How to refer to class which was not yet declared?
file.cpp
Code:
FILE_::FILE_( MyGlobalClass * globInst){
PGlobalInstance = globInst;
}
header:
Code:
class FILE_{
private:
MyGlobalClass * PGlobalInstance;
public:
FILE_( MyGlobalClass * globInst );
static int GetEncoderClsid(
const WCHAR* format,
CLSID* pClsid);
int findFiles(std::string path);
static int IsDirectory(std::string path, char * workingPath);
};
Edit:
This looks correct:
Code:
class FILE_{
private:
MyGlobalClass * PGlobalInstance;
public:
FILE_( MyGlobalClass * globInst );
static int GetEncoderClsid(
const WCHAR* format,
CLSID* pClsid);
int findFiles(std::string path);
int IsDirectory(std::string path, char * workingPath);
};
Re: How to refer to class which was not yet declared?
A class static member function cannot access a non-static member. If you want a static member function to access members of a class instance you will need to pass a pointer to the class instance as part of the static function parameters.
Why do you want IsDirectory to be static?
Re: How to refer to class which was not yet declared?
It should not be static. I changed design, originally it was static. But if I remove static I get this error:
clparser.cpp(22): error C2352: 'FILE_::IsDirectory' : illegal call of non-static member function
Code:
class FILE_{
private:
MyGlobalClass * PGlobalInstance;
public:
FILE_( MyGlobalClass * globInst );
static int GetEncoderClsid(
const WCHAR* format,
CLSID* pClsid);
int findFiles(std::string path);
int IsDirectory(std::string path, char * workingPath);
};
The error line:
Code:
if (FILE_::IsDirectory(arg, PGlobalInstance->workingPath)) {
Edit:
On the other hand if I use static, then I got this error:
file.cpp(113): error C2227: left of '->readDirectoryFiles' must point to class/struct/union/generic type
Code:
PGlobalInstance->readDirectoryFiles = true;
this is in
Code:
int FILE_::IsDirectory(std::string path, char * workingPath){
Re: How to refer to class which was not yet declared?
As IsDirectory is now not static, it can only be referenced via an instance of the class and not via the class name.
Any non-static member has to be referenced via a class instance. Static members are referenced via the class name. If you need to use IsDirectory() without reference to a class instance then it needs to be static which means it cannot directly access any non-static members.
Re: How to refer to class which was not yet declared?
So I should do it like
Code:
if (PGlobalInstance->File->IsDirectory(arg, PGlobalInstance->workingPath)) { ... }
In main file I have this:
Code:
class MyGlobalClass{
public:
std::vector<Src*> sources;
std::vector<Target*> destination;
CLParser CLPars;
FILE_ File;
char * workingPath;
int actualSource;
int actualTarget;
bool getRegEx;
bool readDirectoryFiles;
static int IsDirectory(std::string path);
MyGlobalClass(int argc, char* argv[]);
};
Edit
Yet another error:
error C2819: type 'FILE_' does not have an overloaded member 'operator ->'
Re: How to refer to class which was not yet declared?
Please post the full code as I've lost track as to where things are defined and used.
IsDirectory is a static member of MyGlobalClass and hence can't be referenced from an instance as standard and can't reference an instance.
Re: How to refer to class which was not yet declared?
Not static anymore:
Parse3.cpp:
Code:
#include "stdafx.h"
//...
MyGlobalClass * Obj;
int main(int argc, char* argv[])
{
MyGlobalClass Obj(argc, argv);
}
Re: How to refer to class which was not yet declared?
Please post the full code rather than links to the code. I only look at code that is actually posted here.
Quote:
Not static anymore:
and is there still a compile issue?
You would probably get guidance from more members if you posted on codeguru.
Re: How to refer to class which was not yet declared?
Of sure it is. What code should I post? Here are 10 files
The error is:
error C2819: type 'FILE_' does not have an overloaded member 'operator ->'
file.cpp line 20
Code:
CLParser::CLParser( MyGlobalClass * globInst ){
PGlobalInstance = globInst;
}
void CLParser::ParseArgs(int argc, char* argv[]){
...
if (PGlobalInstance->File->IsDirectory(arg, PGlobalInstance->workingPath)) {
}
}
PGlobalInstance should be pointer to global instance of MyGlobalClass. What does mean this error? I expect the operator is correct.
Re: How to refer to class which was not yet declared?
On StackOverFlow I have found one guy asking similar question:
http://stackoverflow.com/questions/1...ember-operator
I think the answer by Al Crowley should be close, however on his code I am not sure where exactly he suggest to paste this line:
Code:
List * listptr = new List();
In my case, I with to create new instance of FILE_ and CLParser in global.cpp during which the constructors should be fired. Hence I think I should not need the new keyword. I expected that copy constructor should be enough to create the instance in the scope of MyGlobalClass::MyGlobalClass(...) function.
Edit:
I got feeling that problem could be here in the function MyGlobalClass::MyGlobalClass(...):
Code:
CLParser * CLPars( MyGlobalClass );
FILE_ * File( MyGlobalClass );
I am trying to pass a class, but there should be reference to the instance of MyGlobalClass ... And the instance must be create in main file parse.cpp
Re: How to refer to class which was not yet declared?
Yet now in the MyGlobalClass::MyGlobalClass(...) function if I add a pointer to arguments of constructors:
Code:
MyGlobalClass::MyGlobalClass(int argc, char* argv[])
{
CLParser * CLPars( MyGlobalClass *);
FILE_ * File( MyGlobalClass *);
}
I got new error:
error C2512: 'CLParser' : no appropriate default constructor available
error C2512: 'FILE_' : no appropriate default constructor available
Re: How to refer to class which was not yet declared?
Code:
FILE_::FILE_( MyGlobalClass * globInst){
PGlobalInstance = globInst;
}
the constructor to FILE_ takes as argument a pointer to MyGlobalClass. Therefore to use the FILE_ constructor in MyGlobalClass you will need to pass a pointer to the current instance of MyGlobalClass which is the variable 'this' within MyGlobalClass class. So try
Code:
MyGlobalClass::MyGlobalClass(int argc, char* argv[])
{
CLParser * CLPars(this);
FILE_ * File(this);
}
I haven't tried this so you may need to cast this to MyGlobalClass *. The same applies to CLParser. See http://msdn.microsoft.com/en-us/library/y0dddwwd.aspx and other related articles on MSDN and stackoverflow
Re: How to refer to class which was not yet declared?
Yeah, this all I tried already and nothing worked. Same error still. Maybe I should refer to the object by reference and not by pointer?
I read this question: http://stackoverflow.com/questions/6...-c-inner-class
and they use referece, but they are also using nested classes which I don't use. I have my classes separated.
Re: How to refer to class which was not yet declared?
If you merge your .h and .cpp files into one file that demonstrates the problem and post it here I'll have a look.
Re: How to refer to class which was not yet declared?
No need anymore. Compiled. There was initiation list needed for the main class.
Re: How to refer to class which was not yet declared?
Just because the code compiled doesn't mean it is okay. From what I seen there was lots wrong with the structure of your application design. If you want to learn better ways of writing the implementation of your program, post the full code.