Sign in to follow this  

left of '-writeNewObject' must point to class/struct/union

Recommended Posts

JohNny45    122
Hi All Need some help figuring out this error message I get when I compile. It might very well be my use of the extern keyword thats causing the problem... I've got a Log class that I've set to external in my main class // external variables extern Log *log = new Log(); My understanding of the extern keyword is that I should be able to use this log object in any other class in my project. But when I compile I get the following message d:\Visual Studio Projects\...\GUI_Background.cpp(17): error C2227: left of '->writeNewObject' must point to class/struct/union Any ideas? Any help would be greatly appreciated. Thanks ahead of time. -Nny

Share this post

Link to post
Share on other sites
jyk    2094
Although the source of the error here may be fairly obvious, it would still be best to post the specific line of code where the error occurs.

That said, I think the problem is that you're using 'extern' incorrectly (I say "I think' because I haven't used the extern keyword that much myself recently and so may be missing something or other).

Here is what I believe is an example of correct usage:


#ifndef GLOBAL_LOG_H
#define GLOBAL_LOG_H

#include "Log.h"

extern Log* g_log;



#include "Log.h"

Log* g_log;

Then elsewhere in your code, presumably in an initialization function of some sort:
#include "global_log.h"
// ...
g_log = new Log;
Note that the above is untested and may not be error-free.

Share this post

Link to post
Share on other sites
King Mir    2490
I don't see anything wrong with jyk's code. That should work.

You can also manually put the "extern Log* g_log;" into every file that uses it. You don't need it in the file you are initilizing it in though. There you can put "extern Log *log = new Log();", "Log* g_log = new Log();", or "Log* g_log; "but not "extern Log *log".

Obviously jyk's solution is cleaner, but you should read what I said to understand how extern works.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this