Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualeFoDay

Posted 07 August 2012 - 11:40 AM

edited post because I mainly use pointers and then I realized it doesnt work on non-pointers.

this is the error you will get:

In file included from main.cpp:10:0:
Observer.h:16:10: error: field 'myMessage' has incomplete type

[source lang="cpp"]class Message;class Observer { Message myMessage; void accessMessage();}[/source]

so actually this gives no error:

[source lang="cpp"]class Message;class Observer { Message* myMessage; void accessMessage();}[/source]

this actually doesn't give an error message either:

[source lang="cpp"]class Message;class Observer { Message* myMessage; void accessMessage() { myMessage->someFunc(); }}[/source]

But trying to access Observer::accessMessage without including the Message header will give an error:

In file included from main.cpp:10:0:
Observer.h:17:49: error: invalid use of incomplete type 'class Message'
Observer.h:11:7: error: forward declaration of 'class Message'

[source lang="cpp"]#include "Observer.h"int main(int argc, char** argv) { Observer myObserver; myObserver.accessMessage(); // error return 0;}[/source]

To fix the error you will need to include the actual header of Message so it knows about the actual functions.

This is the way I usually set up my files:

[source lang="cpp"]// .h fileclass Message;class Observer { Message* myMessage; void accessMessage();}// .cpp file#include "observer.h"#include "message.h"void Observer::accessMessage() { myMessage->someFunc();}[/source]

And without pointers:

[source lang="cpp"]// .h fileclass Observer { Message myMessage; void accessMessage();}// .cpp file#include "message.h"#include "observer.h"void Observer::accessMessage() { myMessage.someFunc();}[/source]

#2eFoDay

Posted 07 August 2012 - 11:15 AM

edited post because I mainly use pointers and then I realized it doesnt work on non-pointers.

this is the error you will get:

In file included from main.cpp:10:0:
Observer.h:16:10: error: field 'myMessage' has incomplete type

[source lang="cpp"]class Message;class Observer { Message myMessage; void accessMessage();}[/source]

so actually this gives no error:

[source lang="cpp"]class Message;class Observer { Message* myMessage; void accessMessage();}[/source]

you will get an error if you try to do this:

[source lang="cpp"]class Message;class Observer { Message* myMessage; void accessMessage() { myMessage->someFunc(); // Error because it doesn't know about .someFunc() }}[/source]

To fix the error you will need to include the actual header of Message so it knows about the actual functions.

This is the way I usually set up my files:

[source lang="cpp"]// .h fileclass Message;class Observer { Message* myMessage; void accessMessage();}// .cpp file#include "observer.h"#include "message.h"void Observer::accessMessage() { myMessage->someFunc();}[/source]

#1eFoDay

Posted 07 August 2012 - 10:48 AM

you will get no error if you do this:

[source lang="cpp"]class Message;class Observer { Message myMessage; void accessMessage();}[/source]

you will get an error if you try to do this:

[source lang="cpp"]class Message;class Observer { Message myMessage; void accessMessage() { myMessage.someFunc(); // Error because it doesn't know about .someFunc() }}[/source]

To fix the error you will need to include the actual header of Message so it knows about the actual functions.

This is the way I usually set up my files:

[source lang="cpp"]// .h fileclass Message;class Observer { Message myMessage; void accessMessage();}// .cpp file#include "observer.h"#include "message.h"void Observer::accessMessage() { myMessage.someFunc();}[/source]

PARTNERS