[RESOLVED] C++ class definition problems

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Some Moron
Posts: 12
Joined: Sat Jun 04, 2011 7:04 pm

[RESOLVED] C++ class definition problems

Post by Some Moron »

I guess this board is for general programming questions as well, so this seemed like the most appropriate section to post my problem.

I'm trying to use an object-oriented approach for split screen, with a class called View that can be preconfigured to a specific screen area. Its render() function would take its camera and render the contents to the preset region on the screen, as well as any HUD elements.

Code: Select all

class View
{
    public:
        void setPosition(const vector3df &pos);
        void setRotation(const vector3df &rot);
        View(void);
        ~View(void);
        void setFOV(float arc);
        void render(void);
        void setViewport(const core::rect<s32> &area);

    protected:
        ICameraSceneNode *irr_camera;
        ISceneManager* smgr;
        IVideoDriver* driver;
        core::rect<s32> viewport;
};
However, in my Game class, when I try to create a pointer to the View type, the compiler does not recognize View as a type. (Error: "ISO C++ forbids declaration of 'View' with no type")

Below is the Game class as of now; the line that the compiler balks at is "View *camera;". The pointer array (with MAX_PLAYERS) was commented out and replaced with a single pointer to see if that was causing the error; it still happens.

Code: Select all

class game
{
    public:
        game(void);
        ~game(void);
        void renderScene(void);
        void setPlayers(unsigned int numPlayers);
    protected:
        unsigned int players;
        //view* camera[MAX_PLAYERS];
        View *camera;
};
My class definitions seem to match those explained in C++ class tutorials. What am I doing wrong?
Last edited by Some Moron on Sun Jun 05, 2011 4:41 pm, edited 1 time in total.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: C++ class definition problems

Post by randomMesh »

Did you include all headers?

Your code isn't meaningful at all, please post a minimal compilable example which reproduces the problem.
"Whoops..."
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Are you sure you included the header file which contains your View class in the Game class header file? Did you put your View class in a different namespace than the Game class?

Also, try to stick with a correct casing, you should use CamelCase for class names at all times (ie. the beginning letter of each separate word should be capitalized)
Some Moron
Posts: 12
Joined: Sat Jun 04, 2011 7:04 pm

Post by Some Moron »

This is cameraview.h:

Code: Select all

#ifndef __camera_view_h
#define __camera_view_h

#include <irrlicht.h>
#include <iostream>
#include "gameclass.h"

class View
{
    public:
        void setPosition(const vector3df &pos);
        void setRotation(const vector3df &rot);
        View(void);
        ~View(void);
        void setFOV(float arc);
        void render(void);
        void setViewport(const core::rect<s32> &area);

    protected:
        ICameraSceneNode *irr_camera;
        ISceneManager* smgr;
        IVideoDriver* driver;
        core::rect<s32> viewport;
};

void View::setPosition(const vector3df &pos)
{
    irr_camera->setPosition(pos);
}

void View::setRotation(const vector3df &rot)
{
    irr_camera->setRotation(rot);
}

void View::setFOV(float arc)
{
    irr_camera->setFOV(arc);
}

View::View(void)
{
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    irr_camera = smgr->addCameraSceneNodeFPS();
    irr_camera->setFOV(1.0f);
}

View::~View(void)
{
    cout << "\nCamera removed";
}

void View::render()
{
    smgr->setActiveCamera(irr_camera);
    driver->setViewPort(viewport);
    smgr->drawAll();
}

void View::setViewport(const core::rect<s32> &area)
{
   viewport = area;
}

#endif
This is gameclass.h:

Code: Select all

#ifndef __gameclass_h
#define __gameclass_h

#include <irrlicht.h>
#include <iostream>
#include "camera_view.h"

// Maximum number of people who can play split-screen on one system
#define MAX_PLAYERS 4


IrrlichtDevice *device;

class game
{
    public:
        game(void);
        ~game(void);
        void renderScene(void);
        void setPlayers(unsigned int numPlayers);
    protected:
        unsigned int players;
        //view* camera[MAX_PLAYERS];
        View *camera;
};

game::game(void)
{
    unsigned short int i;
    for(i = 0; i < MAX_PLAYERS; i++)
    {
        camera[i] = new view;
    }
}

game::~game(void)
{
    delete[] camera;
}

#endif
Both headers are included in main.cpp. Currently, nothing is enclosed in namespaces - perhaps it would be wise for me to change that?

CamelCase - in other words, RenderScene, SetPlayers, etc? Thank you for letting me know about that issue so early in development!
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Separate your implementations in .cpp files, don't do any implementations in .h files (unless you are unable to in some way), only define your classes in headers

And yes that's the general idea for CamelCase, note that this only applies to classes, methods and variables should use lower camelCase (ie. getValue, setValue, etc.) but you're already doing that
Some people (like myself tbh) prefer to put class variables in CamelCase to make a distinction between local variables and class variables
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Post by zerochen »

the main problem i can see is that you have included gameclass in view and included view in gameclass.
if the compiler process your files he come to #include "gameclass.h" and opened this file. after that he reads the line #include "camera_view.h" and try to opened it too. so you have an infinity loop.
do you really need in cameraview.h the inlcude of gameclass?
if you need it maybe class forward deklaration can help you.
Some Moron
Posts: 12
Joined: Sat Jun 04, 2011 7:04 pm

Post by Some Moron »

After a little experimentation (and moving the implementations to .cpp files), I was finally able to get the project to compile properly. Thank you for the help!
Post Reply