|
-
Jun 22nd, 2014, 04:19 AM
#1
Thread Starter
Member
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.
-
Jun 22nd, 2014, 08:35 AM
#2
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.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 08:57 AM
#3
Thread Starter
Member
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?
-
Jun 22nd, 2014, 09:50 AM
#4
Thread Starter
Member
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?
-
Jun 22nd, 2014, 10:11 AM
#5
Re: How to refer to class which was not yet declared?
 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.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 10:11 AM
#6
Re: How to refer to class which was not yet declared?
 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?
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 10:15 AM
#7
Thread Starter
Member
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);
};
Last edited by CrazyBoy; Jun 22nd, 2014 at 10:25 AM.
-
Jun 22nd, 2014, 10:27 AM
#8
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?
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 11:25 AM
#9
Thread Starter
Member
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){
Last edited by CrazyBoy; Jun 22nd, 2014 at 11:54 AM.
-
Jun 22nd, 2014, 11:54 AM
#10
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.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 12:25 PM
#11
Thread Starter
Member
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 ->'
-
Jun 22nd, 2014, 01:04 PM
#12
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.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 01:32 PM
#13
Thread Starter
Member
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);
}
Last edited by CrazyBoy; Jun 23rd, 2014 at 07:24 AM.
-
Jun 22nd, 2014, 02:28 PM
#14
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.
and is there still a compile issue?
You would probably get guidance from more members if you posted on codeguru.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 22nd, 2014, 03:42 PM
#15
Thread Starter
Member
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.
Last edited by CrazyBoy; Jun 22nd, 2014 at 03:49 PM.
-
Jun 22nd, 2014, 04:07 PM
#16
Thread Starter
Member
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
Last edited by CrazyBoy; Jun 22nd, 2014 at 04:19 PM.
-
Jun 22nd, 2014, 04:21 PM
#17
Thread Starter
Member
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
-
Jun 23rd, 2014, 04:47 AM
#18
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
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 23rd, 2014, 05:33 AM
#19
Thread Starter
Member
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.
-
Jun 23rd, 2014, 05:42 AM
#20
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.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jun 23rd, 2014, 07:25 AM
#21
Thread Starter
Member
Re: How to refer to class which was not yet declared?
No need anymore. Compiled. There was initiation list needed for the main class.
-
Jun 30th, 2014, 01:36 AM
#22
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|