enum as function parameter

Started by
3 comments, last by Sceletor 16 years, 4 months ago
Im trying to use an enum as a parameter in order to tell my render function what kind of content should be rendered. My Problem is now, that no matter what I give as a parameter, gcc just reports an error by saying:
Quote:ScreenObjectsContainer.hpp:28: error: variable or field `render' declared void
Here is the code:
#ifndef SCREENOBJECTSCONTAINER_HPP
#define SCREENOBJECTSCONTAINER_HPP

#include <iostream>
#include <list>

#include "ScreenObject.hpp"

using namespace std;

namespace client
{
    class Client;
    class GraphicModule;

    class ScreenObjectsContainer
    {
        typedef list<ScreenObject*> Screen_List;

        public:
            ScreenObjectsContainer  (Client *new_client, GraphicModule *new_graphic_module);
            ~ScreenObjectsContainer ();

            void register_item  (ScreenObject *new_scr_object);
            void remove_item    (ScreenObject *scr_object, int x_pos, int y_pos);
            void fire_action    (int x_pos, int y_pos);
            void arm_item       (int x_pos, int y_pos);
            void render         (client::GraphicModule::module_state new_state); //HERE is the enum as a parameter

        private:
            GraphicModule   *module;
            Client          *client;

            int screen_width;
            int screen_height;
            bool system_highlighted;

            Screen_List curr_list;
    };
}
#endif

The funny thing is that before I implemented the function, gcc gave me this as an error:
Quote:GraphicModule.cpp:149: error: no matching function for call to `client::ScreenObjectsContainer::render(client::GraphicModule::module_state)'
I guess this is caused by the namespace or the forward declarations, but I got no idea how to fix this, so can anybody help me out?
Advertisement
Well since you're using forward declaration, the compiler has no idea what's inside your GraphicModule, you would have to include the header where the enum is define, or define it in a seperate header.
Quote:Original post by Dancin_Fool
Well since you're using forward declaration, the compiler has no idea what's inside your GraphicModule, you would have to include the header where the enum is define, or define it in a seperate header.


Hwat do you mean with "seperate header"? Do I have to make a new header File or can I just copy/paste a part of the declaration from the forward-declared class into the Header of this File?

A code example would be nice...
The problem is that at the line with the error, the compiler doesn't know what a GraphicModule::module_state is. Presumably, you have defined the enum in a header file named "GraphicModule.h". You should include that header file so that the compiler knows what a GraphicModule::module_state is when it compiles this header file.

A couple more things you can do:
  1. Do not include iostream, nothing in this header file uses it (unless I missed something).
  2. You can forward-declare ScreenObject instead of including the header file.
  3. Do not put using namespace xxx; in a header outside of any scope. It leads to problems. Even though it may seem inconvenient, it is better to prefix the namespace everywhere in a header file (as in typedef std::list<ScreenObject*> Screen_List;).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
The problem is that at the line with the error, the compiler doesn't know what a GraphicModule::module_state is. Presumably, you have defined the enum in a header file named "GraphicModule.h". You should include that header file so that the compiler knows what a GraphicModule::module_state is when it compiles this header file.


The reason why I made a forward declaration is that GraphicModule.hhp already includes ScreenObjectsContainer.hpp, and I dont want a circular include.
So is there another way to tell the compiler about the enumeration in GraphicModule.hpp?

Quote:Original post by JohnBolton
A couple more things you should do:
  1. Do not include iostream, nothing in this header file uses it (unless I missed something).
  2. You can forward-declare ScreenObject instead of including the header file.


Did the first, but the second leads to problems in other classes.

Quote:Original post by JohnBolton
  • Do not put using namespace xxx; in a header outside of any scope. It leads to problems. Even though it may seem inconvenient, it is better to prefix the namespace everywhere in the header file (as in typedef list<ScreenObject*> Screen_List;).


  • Ill *try* to do that once Im done with my other problems.

    This topic is closed to new replies.

    Advertisement