×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

too simple to see

too simple to see

too simple to see

(OP)
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:

Quote:

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)
 

RE: too simple to see

Class C_AR2_Messages, not C_AR2_Messages.h!
It's a file name: C_AR2_Messages.h.
Have you ever seen C++ names?
 

RE: too simple to see

(OP)
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)
 

RE: too simple to see

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: too simple to see

(OP)
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.

Quote:

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)
 

RE: too simple to see

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: too simple to see

(OP)

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)
 

RE: too simple to see

CODE

C_AR2_Messages.h *m_messages;
should be

CODE

C_AR2_Messages *m_messages;

RE: too simple to see

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.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close