Code organising / headers

edited October 2010 in Help request
Hi Gents. My project has been rocketing along nicely for a few weeks now and I have tried to move as much non-orx related c++ variables and functions into code & header files.

I would now like to move things like orxOBJECT declarations into a header (functions too) but I receive errors. I noticed I also got errors for things like STL strings and had to use std::string in the header because including <string> isn't appropriate in a header.

Of course my #include "orxPluginAPI.h" is in my main .cpp file and my header is included from here so I figure I need to do something like the following:
main.cpp

#include "orxPluginAPI.h"

orxSTATUS Init()
{
	pstPlayer = orxObject_CreateFromConfig("Player");

... etc

main.h

#ifndef MAIN_H
#define MAIN_H

orxOBJECT *pstPlayer;
//extern orxOBJECT *pstPlayer; ???

... etc

But I get an error:

syntax error : missing ';' before '*'

This is all due to my limited but growing knowledge of c++. I'm sure the answer is quite basic to seasoned programmers.

Comments

  • edited October 2010
    sausage wrote:
    Hi Gents. My project has been rocketing along nicely for a few weeks now and I have tried to move as much non-orx related c++ variables and functions into code & header files.

    Hi! Nice to see you were able to move forward with your dev. =)
    I would now like to move things like orxOBJECT declarations into a header (functions too) but I receive errors. I noticed I also got errors for things like STL strings and had to use std::string in the header because including <string> isn't appropriate in a header.

    It's a bit late here and I'm a bit tired but I'm not sure what you exactly want to do.
    Of course my #include "orxPluginAPI.h" is in my main .cpp file and my header is included from here so I figure I need to do something like the following:

    So you're using orx's launcher then? If you're building a stand alone (with a main() and Init, Run, Exit callbacks) you should include "orx.h" instead. I personally find more flexible the standalone version but i have to admit it wasn't always the case, especially a couple of years ago when initializing orx wasn't so straightforward. :)
    main.cpp
    
    #include "orxPluginAPI.h"
    
    orxSTATUS Init()
    {
    	pstPlayer = orxObject_CreateFromConfig("Player");
    
    ... etc
    

    main.h
    
    #ifndef MAIN_H
    #define MAIN_H
    
    orxOBJECT *pstPlayer;
    //extern orxOBJECT *pstPlayer; ???
    
    ... etc
    

    But I get an error:

    syntax error : missing ';' before '*'

    This is all due to my limited but growing knowledge of c++. I'm sure the answer is quite basic to seasoned programmers.

    Well, if I'm reading correctly, your main.h has no knowledge of orx whatsoever.
    You need to include orx.h/orxPluginAPI.h in it so that orxOBJECT will be defined. Then in your main.cpp you simply have to include "main.h" instead of any orx headers.

    Maybe that wasn't the answer you expected, but as I said, I'm a bit tired and I might have missed the obvious. =)
  • edited October 2010
    I suppose I did expect something like that answer and it makes sense. But I thought you can't include orxPluginAPI.h in both main.cpp, and main.h? but on trying it:

    * Adding orxPluginAPI.h to main.h gives an "already defined error in main.cpp"
    * Removing it from main.cpp gives an "already defined error in ***.obj"

    But really... I suppose I do have to convert to a standalone project from the launcher eventually anyway. So perhaps I should work on that first and come back to this after.

    But before I do, is it right to say that you can't really split up orx objects and functions into headers with the launcher project include?
  • edited October 2010
    Mmh, the thing is that I'm still unclear about which kind of separation you want to make. :)

    But anyway, the launcher option was only meant for adding new plugins or for prototyping rapidly something within a single .obj.

    As for your errors, did you do a rebuild to make sure no old object file was used for linking?

    Converting to a stand alone should take you less than 5 mins. Your init callback stay the same, the run and exit ones can be empty.
    Then you simply have to add a main function and call orx_Execute() from there. (If you're using a WinMain function for a windows console-less app, there's a orx_WinExecute() that can be used instead). Last step, convert your project to output an exe instead of a library, include "orx.h" instead of "orxPluginAPI.h" and that should be it. =)
  • edited October 2010
    Cool, thanks for that. I'll get my project converted.

    As for splitting up into headers, what I mean is, declaring all my global orxOBJECT variables in the header, and placing all my function definitions into a header.

    But I won't worry about that for now. I think converting to a standalone project is my first step. Everything might fall into place after that.

    Very much appreciate your advice as always.
  • edited October 2010
    Ok I see.
    Let me know if you have any trouble with the conversion. :)

    Glad to be of any help. =)
  • edited October 2010
    Just as feedback, following those steps above I managed to convert to a standalone project very easily and quickly. Many thanks!
  • edited October 2010
    Great!
    I'm glad it worked (and that I didn't forget any steps! ;)).

    You'll see, it'll be much more flexible this way, especially if you want multiplatform support. :)
Sign In or Register to comment.