0

Hello , I a creating program in witch objects are stored in the namespace var in var.h. How do I make the var namespace available to more than one file without having redefinition errors ?

### var.h ###
namespace var
{ ... }
### main.cpp ###

#include "var.h" //multiple redefinition error 

...

### engine.cpp ###

#include "var.h" //multiple redefinition error 

...

### objects.h ###

#include "var.h" //multiple redefinition error 

...
4
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by wwsoft
0

It's not the namespace, it's the stuff in the namespace that gets redefined. Ideally you would put only declarations in a header and definitions in a separate implementation file. But you can help alleviate the problem with inclusion guards.

0

It's not the namespace, it's the stuff in the namespace that gets redefined. Ideally you would put only declarations in a header and definitions in a separate implementation file. But you can help alleviate the problem with inclusion guards.

can you give me an example of that ?

-1

Assuming that your code was showing multiple files
including the same file indirectly

by adding the preprocessor line to the top of all of your .h files

#pragma once

you can ensure that no single file includes the same file twice.

0

well , I have 2 header files that include each other objects.h and vars.h. The #pragma once is causing var to ignore objects.h from being included ... So whats the solution for this , merge them ? or is there some other command that is used to combat this from happening ?

Edited by wwsoft: n/a

0

I have merged vars & objects but I get is error from this code

namespace var
{
    vector<game_object*> _objects_; //error , vector is included ... 
    vector<SDL_Surface*> _img_; //error
    vector<TTF_Font*> _font_; //error
    vector<Mix_Chunk*> _sound_; //error
    vector<Mix_Music*> _music_; //error
    

    bool quit=0;
    bool pause=0;
    int sound_volume=100;
    int music_volume=100;
}
### Error ###
error: expected constructor, destructor, or type conversion before ‘<’ token
error: expected constructor, destructor, or type conversion before ‘<’ token
error: expected constructor, destructor, or type conversion before ‘<’ token
error: expected constructor, destructor, or type conversion before ‘<’ token
error: expected constructor, destructor, or type conversion before ‘<’ token

---
darn cant edit my posts after 1/2 hour ...

0

I am using a different version of compiler so different errors but
have you scoped vector?
the std::

//var.h
#pragma once
#include <vector>
namespace var
{
std::vector<int *> vpi;
std::vector<double *> vdi
}

works for me.

0

works , but .... even with "#pragma once" on top Im getting

...
In function main
multiple definition of `var::_objects_
first defined here
In function main
multiple definition of var::_img_
first defined here
In function main
multiple definition of var::_font_
first defined here
In function main
multiple definition of var::_sound_
first defined here
In function main
first defined here|
In function main
multiple definition of var::quit
first defined here
In function main
multiple definition of var::pause
first defined here
...

obejcts.h

#pragma once

#ifndef OBJECTS_H_INCLUDED
#define OBJECTS_H_INCLUDED


....

using namespace std;

class game_object
....

class cpong1 : public game_object
....


namespace var
{
    vector<game_object*> _objects_;
    vector<SDL_Surface*> _img_;
    vector<TTF_Font*> _font_;
    vector<Mix_Chunk*> _sound_;
    vector<Mix_Music*> _music_;


    bool quit=0;
    bool pause=0;
    int sound_volume=100;
    int music_volume=100;
}

#endif // OBJECTS_H_INCLUDED

any advice ?

0

Assuming that in all your own .h you have #pragma once

If you rename _objects_ to _abcdefgh_1234
do you get the same error with "_objects_"
replaced with _abcdefgh_1234 ?

if not then you have two places where you have used the same function name and this needs to be fixed it could be var:: is used elsewhere

I always uses classes and don't use the using so as not to confuse
visio intellisense or risk polluting the namespace

I am not familiar with the SDL libraries that you are using
but I assume that you are trying to get some game to work
without using the windows api.

It might be missing something obvious... but there is a lot of
includes and code that are not being shown here and an equivalent
code for me does not give an error

works , but .... even with "#pragma once" on top Im getting

...
In function main
multiple definition of `var::_objects_
first defined here
In function main
multiple definition of var::_img_
first defined here
In function main
multiple definition of var::_font_
first defined here
In function main
multiple definition of var::_sound_
first defined here
In function main
first defined here|
In function main
multiple definition of var::quit
first defined here
In function main
multiple definition of var::pause
first defined here
...

obejcts.h

#pragma once

#ifndef OBJECTS_H_INCLUDED
#define OBJECTS_H_INCLUDED


....

using namespace std;

class game_object
....

class cpong1 : public game_object
....


namespace var
{
    vector<game_object*> _objects_;
    vector<SDL_Surface*> _img_;
    vector<TTF_Font*> _font_;
    vector<Mix_Chunk*> _sound_;
    vector<Mix_Music*> _music_;


    bool quit=0;
    bool pause=0;
    int sound_volume=100;
    int music_volume=100;
}

#endif // OBJECTS_H_INCLUDED

any advice ?

0

the vector _DEBUG_ and int pie I included is also included in the errors

This is All the messy code in objects.h is anything horribly wrong with it ?

are there any better ways to make vars available globally ?

#pragma once

#ifndef OBJECTS_H_INCLUDED
#define OBJECTS_H_INCLUDED

#include <SDL.h>
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"

#include <cstdio>
#include <vector>
#include <iostream>
#include "engin.h"

using namespace std;

class game_object
{
    public:

    SDL_Surface* sprite;
    SDL_Rect rect;
    std::string type[3];//controller , group , name

    int x;
    int y;
    int z;
    bool dead;

    virtual void update()
    {
    }


    std::string get_type(int typen)
    {
        if (typen==0)
            return type[0];
        if (typen==1)
            return type[1];
        if (typen==2)
            return type[2];
    }

    SDL_Surface * get_sprite()
    {
        return sprite;
    }

    SDL_Rect get_rect()
    {
        return rect;
    }

};

class cpong1 : public game_object
{
    public:

    cpong1( int xx , int yy , int zz )
    {
        x=xx;
        y=yy;
        z=zz;

        type[0]="player"; //controller
        type[1]="pong"; //object group
        type[2]="cpong1"; //object
        dead=0;

        sprite=SDL_CreateRGBSurface(SDL_HWSURFACE, 16, 48, 8, 255, 255, 255, 255);
        SDL_FillRect(sprite, 0, (180,180,180));
        rect.w=sprite->w;
        rect.h=sprite->h;
        rect.x=x;
        rect.y=y;
    }

    virtual void update()
    {
        Uint8 *keystate = SDL_GetKeyState(NULL);
        if ( keystate[SDLK_UP] )
            y-=5;
        if ( keystate[SDLK_DOWN] )
            y+=5;

//            drip_rect_collide_all(rect,var::_objects_,0);

    }

    ~cpong1()
    {
        SDL_FreeSurface(sprite);
    }
};




namespace var
{
    vector<game_object*> _objects_;
    vector<SDL_Surface*> _img_;
    vector<TTF_Font*> _font_;
    vector<Mix_Chunk*> _sound_;
    vector<Mix_Music*> _music_;


    bool quit=0;
    bool pause=0;
    int sound_volume=100;
    int music_volume=100;
}

#endif // OBJECTS_H_INCLUDED

Edited by wwsoft: n/a

0

Use the extern keyword to introduce the variables inside the header (.h) files. Define these variables in their counterpart .cpp files.

For example, your header could be

#ifndef OBJECT_H_DEFINED
#define OBJECT_H_DEFINED

// file: object.h

// std::vector is needed in this header file
#include <vector>

// Introduce what is exposed by the 'var' namespace 
namespace var
{
    // You cannot have variables defined in a header file, which
    // is included by multiple .cpp files. So they need to be extern ...
    extern std::vector<game_object*> _objects_;
    extern std::vector<SDL_Surface*> _img_;
    extern std::vector<TTF_Font*> _font_;
    extern bool quit;
    extern bool pause;
    extern int sound_volume;
    // etc ...

    // An ordinary function declaration, no need for extern
    void ordinary_function();

    // A class declaration
    class some_class
    {
        // ctor
        some_class();
    };
}
#endif // ndef OBJECT_H_DEFINED

Then the .cpp file would be

// file: object.cpp

#include "object.h"
namespace var
{
    std::vector<game_object*> _objects_;
    std::vector<SDL_Surface*> _img_;
    std::vector<TTF_Font*> _font_;

    bool quit = 0;
    bool pause = 0;
    int sound_volume = 100;

    void ordinary_function()
    {
         // something here
    }

    // class definition ...
    some_class::some_class()
    {}
}

[EDIT]
The above is to demonstrate the basic idea of putting these things together, so given that there are e.g. SDL_Surface * and such, you also need the related headers for those included in proper places. But I hope that you get the basic idea though.

Edited by mitrmkar: ordinary_function()

0

Thank you ! I had been trying to work through that for a week :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.