Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

too simple to see

Status
Not open for further replies.

bkelly13

Programmer
Aug 31, 2006
98
US
VS 2008, C++, Windows XP, MFC project
In the main application file C_AR2_MessagesAppDlg.h is:
Code:
#include "C_AR2_Configuration.h" // class of same name
#include "C_AR2_Messages.h"      // ditto
...
public:
C_AR2_Configuration *m_configuration;  // OK
C_AR2_Messages.h *m_messages;          // syntax error

The point is to declare pointers to the classes as they will be used for a while then deleted. The first one is fine, the second one solicits the error:
C2143: syntax error: missing ; before *
When the second is commented out there are no compile errors. I presume that the compiler cannot see class C_AR2_Messages. I have carefully compared names and spellings. I even used copy paste to put the class name into the declaration to avoid any typos. I went back and performed an add existing item for the class for both the .h and .cpp file to no effect.
There is something simple prohibiting the compiler from seeing this class and I just cannot detect it.


We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Class C_AR2_Messages, not C_AR2_Messages.h!
It's a file name: C_AR2_Messages.h.
Have you ever seen C++ names?
 
I don't see an edit function in this forum so I must add a reply. I have partially figured this out. The new class needs to reference some types declared in the original. That results in each one haveing a include for the other. When I edit C_AR2_Messages and remove the include for C_AR2_MessagesAppDlg.h it compiles OK.

I thought this was legal, but I conclude that with the #pragma once it can cause a problem.

For now I think I can extract all the common declarations to a separate file and both will include it. But for the future, what do I do when each class needs to reference the other?

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
I could not understand the OP question:

Q1. Where is an error in (see OP snippet senseless declaration;):
Code:
C_AR2_Messages.h *m_messages;          // syntax error
A1. Correct class name.

Q2. What do I do when each class needs to reference the other?
A2. Use forward declaration:
Code:
class B; // B is a class name: forward declaration.
class A
{
...
B* ... // That's OK...
B& ... // It's correct ref to B...
// Of course, you can't declare B objects or use B members here.
};
...
class B
{
...
};
 
RE: Q1. Where is an error in (see OP snippet senseless declaration;):CODEC_AR2_Messages.h *m_messages; // syntax errorA1. Correct class name.

The problem is that in file C_AR2_MessagesAppDlg.h the compiler did not accept any declarations from C_AR2_MessagesAppDlg.h and class C_AR2_Messages. My interperation is that because both files inclued each other the #pragma once was getting in there and was it not the case the each include file could read the other.

Turns out there is one declaration that is needed by both so I extracted that out to another file. Now the C_AR2_MessagesAppDlg.h (the MFC main application) references the C_AR2_Messages.h (My added class) file but not the other way around.

B is a class name: forward declaration.
I did not think about a forward declaration. But then again, if it cannot see the include file, a forward declaration won't do much good.

This was frustrating because it was all so simple, everything was indeed present, it just would not compile.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Sorry, I can't understand your problem.

I can't understand statements
"in the file C_AR2_MessagesAppDlg.h the compiler did not accept any declarations from C_AR2_MessagesAppDlg.h and class C_AR2_Messages"
and
"it cannot see the include file".

You have bad declaration and syntax error in the OP snippet, then you are writing about mysterious "can't see" problems.

If header files "include each other" - it's an error, it's impossible in C and C++.
 

RE: C_AR2_Messages.h *m_messages;
and
RE: You have bad declaration and syntax error in the OP snippet, then you are writing about mysterious "can't see" problems.

You are correct, until you mentioned it I just could not see that error (the one in my OP). That's a typo in creating the post because I cannot copy from my work comptuer to another computer.

I was thinking you can cross reference classes, each referencing and being able to instantiate or call members of the other class.

As I write, I think that is the intent of the comment on the forward declaration. But i don't know how that declaration will work if both classes cannot include the other's .h file.



We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Code:
C_AR2_Messages[COLOR=red][b].h[/b][/color] *m_messages;
should be
Code:
C_AR2_Messages *m_messages;
 
Post examples of these two classes definitions mutually dependent parts. With forward declarations you can use references and pointers to another class. No problems with mutual depenedent class implementations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top