LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   CPP: Multiple Defintion Error. (http://www.linuxquestions.org/questions/programming-9/cpp-multiple-defintion-error-550490/)

RHLinuxGUY 05-01-2007 07:11 PM

CPP: Multiple Defintion Error.
 
I am having a multiple definition error when I try to compile my program.

I'm not expecting anyone to look through each line of each file. It is just some people would like to know key information, and at the moment I have to finish up school which is taking up time, so I probably will not always be able to edit this post over and over again within a small period of time. Sorry for the inconvenience.

Here is the files that I have:

Header Files:
sc_object.h
sc_obstacle.h - requires: sc_object.h
sc_game.h - requires: sc_object.h : sc_obstacle.h

Source Files:
sc_functions.cpp - requires: sc_object.h : sc_obstacle.h
sc_game.cpp - requires: all the header files.

Here is my Makefile, followed by the source of all the files.

BTW, more than likely you don't need to see the whole of all class files, because someone on an IRC chatroom, told me that if I had any inline functions in my class files, but never responded back. So I thought maybe someone would just skim the functions prototypes and virtual functions to see if I have them or not for whatever reason.

I assumed that it may have been the problem, from the guy in the chatroom, though, the virtual functions, if I don't want to implement them by hand in each sub-class, would have to be inline. Since defining them outside the class would cause problems.

Anyways:

Makefile:
Code:

SDLCFLAGS=`sdl-config --cflags`
SDL=`sdl-config --cflags --libs` -lSDL -lSDL_image
CPPARGS=-W -Wall -ansi

sc_game: sc_functions.o sc_game.o
        g++ -g sc_functions.o sc_game.o ${CPPARGS} ${SDL} -o sc_game

sc_game.o: sc_game.cpp sc_object.h sc_obstacle.h
        g++ -g -c sc_game.cpp ${CPPARGS} ${SDLCFLAGS}

sc_functions.o: sc_functions.cpp sc_object.h sc_obstacle.h
        g++ -g -c sc_functions.cpp ${CPPARGS} ${SDLCFLAGS}

Header File 1: sc_object.h
Code:

#ifndef scobject_h__
#define scobject_h__

#include <SDL/SDL.h>
#include <iostream>
#include <string>
#include <vector>

class _scobject
{
private:

 // The x y location, as well as the width and height
 // of the object.

        SDL_Rect xywh;

 // If staticPosition == true, object stays in a fixed position.
 
        bool staticPosition;

 // If collision == false, than the object will not worry about collision.

        bool collision;

 // If staticPosition == false, than the object can move specifed in
 // a direction.
 // left == (move_x == 1), right == (move_x == 2), stop x movement == (move_x == 0)
 // up == (move_y == 1), down == (move_y == 2) stop y movement == (move_y == 0)
 
        int move_x;
        int move_y;

 // The boxed area the object stays in. (a clipped(SDL_ClipRect) area.*
 // Default value should be -1.  Any attempt to blit to an area is(should be) thwarted.
 
        int boxArea;

 // Clip the image or not within it's specified boxArea.
 
        bool clipImage;

 // The type of scobject it is. *
 
        int type;

 // False for not viewable, true otherwise.

        bool viewable;

public:

 // insert data functions
        void chg_boxArea  ( int );
        void chg_clipImage ( bool );
        void chg_viewable  ( bool );
        void chg_position  ( Sint16, Sint16 );
        void chg_perimeter ( Sint16, Sint16 );
        void chg_type      ( int );
        void chg_dir_x    ( int );
        void chg_dir_y    ( int );
        void chg_collision ( bool );

 // retrieve data functions
        int      get_boxArea  ();
        bool    get_clipImage ();
        bool    get_viewable  ();
        SDL_Rect get_position  ();
        SDL_Rect get_perimeter ();
        int      get_type      ();
        int      get_dir_x    ();
        int      get_dir_y    ();
        bool    get_collision ();

 // virtual functions. Cluttering sucks. :(

        // any object that displays an image
        virtual void show                      ( void            ) {}
        virtual void chg_blitSurface          ( SDL_Surface *    ) {}
        virtual void chg_images                (                  ) {}
        virtual void chg_addImageBack          ( int              ) {}
        virtual void chg_rmImageBack          ( int              ) {}
        virtual void chg_loadImage            ( std::string, int ) {} // the file name, and index number.
        virtual void chg_imageAddTime          (                  ) {} // just add SDL_GetTicks() + imageTimeToAdd to imageTimeGoal.
        virtual void chg_imageTimeGoal        ( Uint32          ) {} // Specify your own time to add to imageTimeGoal.
        virtual void chg_imageSpecifyTimeToAdd ( Uint32          ) {} // Specify your own imageTimeToAdd.

        virtual SDL_Surface * get_blitSurface    () { return 0; }
        virtual int              get_currentImage  () { return 0; }
        virtual Uint32        get_imageTimeGoal  () { return 0; }
        virtual Uint32              get_imageTimeToAdd () { return 0; }

        // any object with human like traits (health, speed, attack, etc.)
        virtual void chg_danger ( bool ) {}
        virtual void chg_attack ( int  ) {}
        virtual void chg_hp    ( int  ) {}

        virtual bool get_dangerous () { return 0; }
        virtual int  get_attack    () { return 0; }
        virtual int  get_hp        () { return 0; }

        // any object that only has a one dimensional array of images.
        virtual std::vector< SDL_Surface* > get_images () { std::vector< SDL_Surface* > images_null; return images_null; }

 // other functions
        void move ();

        _scobject  ();
        virtual ~_scobject () {}
};

_scobject::_scobject ()
{
        collision = false;
        staticPosition = true;
        move_x = 0;
        move_y = 0;
        clipImage = true;
        viewable = true;
        boxArea = -1;
}

 // insert data functions
void _scobject::chg_boxArea  ( int boxArea_arg )    { boxArea = boxArea_arg; }
void _scobject::chg_clipImage ( bool clipImage_arg ) { clipImage = clipImage_arg; }
void _scobject::chg_viewable  ( bool viewable_arg )  { viewable = viewable_arg; }
void _scobject::chg_position  ( Sint16 x_arg,
                                Sint16 y_arg )      { xywh.x = x_arg; xywh.y = y_arg; }
void _scobject::chg_perimeter ( Sint16 w_arg,
                                Sint16 h_arg )      { xywh.w = w_arg; xywh.h = h_arg; }
void _scobject::chg_type      ( int type_arg )      { type = type_arg; }
void _scobject::chg_dir_x    ( int move_x_arg )    { move_x = move_x_arg; }
void _scobject::chg_dir_y    ( int move_y_arg )    { move_y = move_y_arg; }
void _scobject::chg_collision ( bool collision_arg ) { collision = collision_arg; }

 // retrieve data functions
int      _scobject::get_boxArea  () { return boxArea; }
bool    _scobject::get_clipImage () { return clipImage; }
bool    _scobject::get_viewable  () { return viewable; }
SDL_Rect _scobject::get_position  () { return xywh; }
SDL_Rect _scobject::get_perimeter () { return xywh; }
int      _scobject::get_type      () { return type; }
int      _scobject::get_dir_x    () { return move_x; }
int      _scobject::get_dir_y    () { return move_y; }
bool    _scobject::get_collision () { return collision; }

 // other functions
void _scobject::move ()
{
        if      ( move_x == 1 ) { xywh.x--; }
        else if ( move_x == 2 ) { xywh.x++; }
        if      ( move_y == 1 ) { xywh.y--; }
        else if ( move_y == 2 ) { xywh.y++; }
}

#endif

Header File 2: sc_obstacle.h
Code:

#ifndef scobject_h__
#define scobject_h__

#include <SDL/SDL.h>
#include <iostream>
#include <string>
#include <vector>

class _scobject
{
private:

 // The x y location, as well as the width and height
 // of the object.

        SDL_Rect xywh;

 // If staticPosition == true, object stays in a fixed position.
 
        bool staticPosition;

 // If collision == false, than the object will not worry about collision.

        bool collision;

 // If staticPosition == false, than the object can move specifed in
 // a direction.
 // left == (move_x == 1), right == (move_x == 2), stop x movement == (move_x == 0)
 // up == (move_y == 1), down == (move_y == 2) stop y movement == (move_y == 0)
 
        int move_x;
        int move_y;

 // The boxed area the object stays in. (a clipped(SDL_ClipRect) area.*
 // Default value should be -1.  Any attempt to blit to an area is(should be) thwarted.
 
        int boxArea;

 // Clip the image or not within it's specified boxArea.
 
        bool clipImage;

 // The type of scobject it is. *
 
        int type;

 // False for not viewable, true otherwise.

        bool viewable;

public:

 // insert data functions
        void chg_boxArea  ( int );
        void chg_clipImage ( bool );
        void chg_viewable  ( bool );
        void chg_position  ( Sint16, Sint16 );
        void chg_perimeter ( Sint16, Sint16 );
        void chg_type      ( int );
        void chg_dir_x    ( int );
        void chg_dir_y    ( int );
        void chg_collision ( bool );

 // retrieve data functions
        int      get_boxArea  ();
        bool    get_clipImage ();
        bool    get_viewable  ();
        SDL_Rect get_position  ();
        SDL_Rect get_perimeter ();
        int      get_type      ();
        int      get_dir_x    ();
        int      get_dir_y    ();
        bool    get_collision ();

 // virtual functions. Cluttering sucks. :(

        // any object that displays an image
        virtual void show                      ( void            ) {}
        virtual void chg_blitSurface          ( SDL_Surface *    ) {}
        virtual void chg_images                (                  ) {}
        virtual void chg_addImageBack          ( int              ) {}
        virtual void chg_rmImageBack          ( int              ) {}
        virtual void chg_loadImage            ( std::string, int ) {} // the file name, and index number.
        virtual void chg_imageAddTime          (                  ) {} // just add SDL_GetTicks() + imageTimeToAdd to imageTimeGoal.
        virtual void chg_imageTimeGoal        ( Uint32          ) {} // Specify your own time to add to imageTimeGoal.
        virtual void chg_imageSpecifyTimeToAdd ( Uint32          ) {} // Specify your own imageTimeToAdd.

        virtual SDL_Surface * get_blitSurface    () { return 0; }
        virtual int              get_currentImage  () { return 0; }
        virtual Uint32        get_imageTimeGoal  () { return 0; }
        virtual Uint32              get_imageTimeToAdd () { return 0; }

        // any object with human like traits (health, speed, attack, etc.)
        virtual void chg_danger ( bool ) {}
        virtual void chg_attack ( int  ) {}
        virtual void chg_hp    ( int  ) {}

        virtual bool get_dangerous () { return 0; }
        virtual int  get_attack    () { return 0; }
        virtual int  get_hp        () { return 0; }

        // any object that only has a one dimensional array of images.
        virtual std::vector< SDL_Surface* > get_images () { std::vector< SDL_Surface* > images_null; return images_null; }

 // other functions
        void move ();

        _scobject  ();
        virtual ~_scobject () {}
};

_scobject::_scobject ()
{
        collision = false;
        staticPosition = true;
        move_x = 0;
        move_y = 0;
        clipImage = true;
        viewable = true;
        boxArea = -1;
}

 // insert data functions
void _scobject::chg_boxArea  ( int boxArea_arg )    { boxArea = boxArea_arg; }
void _scobject::chg_clipImage ( bool clipImage_arg ) { clipImage = clipImage_arg; }
void _scobject::chg_viewable  ( bool viewable_arg )  { viewable = viewable_arg; }
void _scobject::chg_position  ( Sint16 x_arg,
                                Sint16 y_arg )      { xywh.x = x_arg; xywh.y = y_arg; }
void _scobject::chg_perimeter ( Sint16 w_arg,
                                Sint16 h_arg )      { xywh.w = w_arg; xywh.h = h_arg; }
void _scobject::chg_type      ( int type_arg )      { type = type_arg; }
void _scobject::chg_dir_x    ( int move_x_arg )    { move_x = move_x_arg; }
void _scobject::chg_dir_y    ( int move_y_arg )    { move_y = move_y_arg; }
void _scobject::chg_collision ( bool collision_arg ) { collision = collision_arg; }

 // retrieve data functions
int      _scobject::get_boxArea  () { return boxArea; }
bool    _scobject::get_clipImage () { return clipImage; }
bool    _scobject::get_viewable  () { return viewable; }
SDL_Rect _scobject::get_position  () { return xywh; }
SDL_Rect _scobject::get_perimeter () { return xywh; }
int      _scobject::get_type      () { return type; }
int      _scobject::get_dir_x    () { return move_x; }
int      _scobject::get_dir_y    () { return move_y; }
bool    _scobject::get_collision () { return collision; }

 // other functions
void _scobject::move ()
{
        if      ( move_x == 1 ) { xywh.x--; }
        else if ( move_x == 2 ) { xywh.x++; }
        if      ( move_y == 1 ) { xywh.y--; }
        else if ( move_y == 2 ) { xywh.y++; }
}

#endif

Header File 3: sc_game.h
Code:

#ifndef scgame_h__
#define scgame_h__

#include <SDL/SDL.h>
#include "SDL_image.h"
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

#include "sc_object.h"
#include "sc_obstacle.h"

enum _objectType { SC_OBJECT, SC_OBSTACLE };

class _scgame
{
private:

// Variables

 // Games states tell whether the keyboard focus should be on
 // something else and/or if the game should pause for something.
 // if gameState == 0 : game is in menu, 1 : game is in a 'game'.
 
        int gameState;

 // objIndex serves two purposes.  It holds information for any
 // object derived from _scobject, and acts like a z-index.

        std::vector< _scobject * > objIndex;

 // Main surface where objects blit their images too.
 
        SDL_Surface * window;

// Functions

        void show ();

public:

        _scgame  ();
        ~_scgame ();

        // Main Loop
        void init ();
};

_scgame::_scgame ()
{
        SDL_Init ( SDL_INIT_VIDEO );

        atexit (SDL_Quit);

        window = SDL_SetVideoMode ( 800, 600, 16, SDL_SWSURFACE );

        gameState = 1;
}

_scgame::~_scgame ()
{
}

// Private Functions
void _scgame::show ()
{
        SDL_FillRect ( window, NULL, SDL_MapRGB ( window->format, 255, 255, 255) );

        for ( int a_iter = 0; a_iter < objIndex.size(); ++a_iter )
        {
                objIndex [a_iter]->show ();
        }

        SDL_Flip (window);
}

// Public Functions
void _scgame::init ()
{
 // Will need to finish _scobstacle::chg_images()
 // so there will be no need to allocate images
 // manually.

        objIndex.resize (1);

        objIndex[0] = sc_new_scobstacle ();

        objIndex[0]->chg_addImageBack ( 1 );
        objIndex[0]->chg_loadImage    ( "images/block.png", -1 );
        objIndex[0]->chg_boxArea      (1);
        objIndex[0]->chg_position    (24, 34);
        objIndex[0]->chg_perimeter    (50, 15);
        objIndex[0]->chg_imageSpecifyTimeToAdd ( 100 );
        objIndex[0]->chg_imageAddTime ();
        objIndex[0]->chg_blitSurface  ( window );
       
        SDL_Event event;

        while (true)
        {
                while ( SDL_PollEvent (&event) ) {
                        if ( event.type == SDL_KEYDOWN ) {
                                switch ( event.key.keysym.sym ) {
                                        case SDLK_ESCAPE: return; break;
                                        default:                  break;
                                }
                        }
                }

                show ();
        }
}

#endif

Source File 1: sc_functions.cpp
Code:

#include <SDL/SDL.h>
#include "SDL_image.h"
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

#include "sc_obstacle.h"

 // full definitions for new object calls

_scobstacle * sc_new_scobstacle () { return (new _scobstacle); }

std::string scitoa ( int to_convert )
{
        std::ostringstream conversion;
       
        conversion << to_convert;

        return conversion.str();
}

Source File 2: sc_game.cpp
Code:

#include <SDL/SDL.h>
#include "SDL_image.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

#include "sc_game.h"

int main ( int argc, char ** argv )
{
        _scgame scgame;

        scgame.init ();

        return 0;
}


RHLinuxGUY 05-01-2007 07:12 PM

Here is the error output when I run make, which all it pretty much is saying is that
certain class functions were already defined in sc_function.cpp.:

Code:

$ make
g++ -g sc_functions.o sc_game.o -W -Wall -ansi `sdl-config --cflags --libs` -lSDL -lSDL_image -o sc_game
sc_game.o: In function `_scobject::_scobject()':
/home/george/SpaceCarnage/040107/sc_object.h:122: multiple definition of `_scobject::_scobject()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:122: first defined here
sc_game.o: In function `_scobject::_scobject()':
/home/george/SpaceCarnage/040107/sc_object.h:122: multiple definition of `_scobject::_scobject()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:122: first defined here
sc_game.o: In function `_scobject::chg_boxArea(int)':
/home/george/SpaceCarnage/040107/sc_object.h:133: multiple definition of `_scobject::chg_boxArea(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:133: first defined here
sc_game.o: In function `_scobject::chg_clipImage(bool)':
/home/george/SpaceCarnage/040107/sc_object.h:134: multiple definition of `_scobject::chg_clipImage(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:134: first defined here
sc_game.o: In function `_scobject::chg_viewable(bool)':
/home/george/SpaceCarnage/040107/sc_object.h:135: multiple definition of `_scobject::chg_viewable(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:135: first defined here
sc_game.o: In function `_scobject::chg_position(short, short)':
/home/george/SpaceCarnage/040107/sc_object.h:137: multiple definition of `_scobject::chg_position(short, short)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:137: first defined here
sc_game.o: In function `_scobject::chg_perimeter(short, short)':
/home/george/SpaceCarnage/040107/sc_object.h:139: multiple definition of `_scobject::chg_perimeter(short, short)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:139: first defined here
sc_game.o: In function `_scobject::chg_type(int)':
/home/george/SpaceCarnage/040107/sc_object.h:140: multiple definition of `_scobject::chg_type(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:140: first defined here
sc_game.o: In function `_scobject::chg_dir_x(int)':
/home/george/SpaceCarnage/040107/sc_object.h:141: multiple definition of `_scobject::chg_dir_x(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:141: first defined here
sc_game.o: In function `_scobject::chg_dir_y(int)':
/home/george/SpaceCarnage/040107/sc_object.h:142: multiple definition of `_scobject::chg_dir_y(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:142: first defined here
sc_game.o: In function `_scobject::chg_collision(bool)':
/home/george/SpaceCarnage/040107/sc_object.h:143: multiple definition of `_scobject::chg_collision(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:143: first defined here
/home/george/SpaceCarnage/040107/sc_object.h:122: multiple definition of `_scobject::_scobject()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:122: first defined here
sc_game.o: In function `_scobject::chg_boxArea(int)':
/home/george/SpaceCarnage/040107/sc_object.h:133: multiple definition of `_scobject::chg_boxArea(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:133: first defined here
sc_game.o: In function `_scobject::chg_clipImage(bool)':
/home/george/SpaceCarnage/040107/sc_object.h:134: multiple definition of `_scobject::chg_clipImage(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:134: first defined here
sc_game.o: In function `_scobject::chg_viewable(bool)':
/home/george/SpaceCarnage/040107/sc_object.h:135: multiple definition of `_scobject::chg_viewable(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:135: first defined here
sc_game.o: In function `_scobject::chg_position(short, short)':
/home/george/SpaceCarnage/040107/sc_object.h:137: multiple definition of `_scobject::chg_position(short, short)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:137: first defined here
sc_game.o: In function `_scobject::chg_perimeter(short, short)':
/home/george/SpaceCarnage/040107/sc_object.h:139: multiple definition of `_scobject::chg_perimeter(short, short)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:139: first defined here
sc_game.o: In function `_scobject::chg_type(int)':
/home/george/SpaceCarnage/040107/sc_object.h:140: multiple definition of `_scobject::chg_type(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:140: first defined here
sc_game.o: In function `_scobject::chg_dir_x(int)':
/home/george/SpaceCarnage/040107/sc_object.h:141: multiple definition of `_scobject::chg_dir_x(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:141: first defined here
sc_game.o: In function `_scobject::chg_dir_y(int)':
/home/george/SpaceCarnage/040107/sc_object.h:142: multiple definition of `_scobject::chg_dir_y(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:142: first defined here
sc_game.o: In function `_scobject::chg_collision(bool)':
/home/george/SpaceCarnage/040107/sc_object.h:143: multiple definition of `_scobject::chg_collision(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:143: first defined here
sc_game.o: In function `_scobject::get_boxArea()':
/home/george/SpaceCarnage/040107/sc_object.h:146: multiple definition of `_scobject::get_boxArea()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:146: first defined here
sc_game.o: In function `_scobject::get_clipImage()':
/home/george/SpaceCarnage/040107/sc_object.h:147: multiple definition of `_scobject::get_clipImage()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:147: first defined here
sc_game.o: In function `_scobject::get_viewable()':
/home/george/SpaceCarnage/040107/sc_object.h:148: multiple definition of `_scobject::get_viewable()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:148: first defined here
sc_game.o: In function `_scobject::get_position()':
/home/george/SpaceCarnage/040107/sc_object.h:149: multiple definition of `_scobject::get_position()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:149: first defined here
sc_game.o: In function `_scobject::get_perimeter()':
/home/george/SpaceCarnage/040107/sc_object.h:150: multiple definition of `_scobject::get_perimeter()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:150: first defined here
sc_game.o: In function `_scobject::get_type()':
/home/george/SpaceCarnage/040107/sc_object.h:151: multiple definition of `_scobject::get_type()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:151: first defined here
sc_game.o: In function `_scobject::get_dir_x()':
/home/george/SpaceCarnage/040107/sc_object.h:152: multiple definition of `_scobject::get_dir_x()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:152: first defined here
sc_game.o: In function `_scobject::get_dir_y()':
/home/george/SpaceCarnage/040107/sc_object.h:153: multiple definition of `_scobject::get_dir_y()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:153: first defined here
sc_game.o: In function `_scobject::get_collision()':
/home/george/SpaceCarnage/040107/sc_object.h:154: multiple definition of `_scobject::get_collision()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:154: first defined here
sc_game.o: In function `_scobject::move()':
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:143: first defined here
sc_game.o: In function `_scobject::get_boxArea()':
/home/george/SpaceCarnage/040107/sc_object.h:146: multiple definition of `_scobject::get_boxArea()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:146: first defined here
sc_game.o: In function `_scobject::get_clipImage()':
/home/george/SpaceCarnage/040107/sc_object.h:147: multiple definition of `_scobject::get_clipImage()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:147: first defined here
sc_game.o: In function `_scobject::get_viewable()':
/home/george/SpaceCarnage/040107/sc_object.h:148: multiple definition of `_scobject::get_viewable()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:148: first defined here
sc_game.o: In function `_scobject::get_position()':
/home/george/SpaceCarnage/040107/sc_object.h:149: multiple definition of `_scobject::get_position()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:149: first defined here
sc_game.o: In function `_scobject::get_perimeter()':
/home/george/SpaceCarnage/040107/sc_object.h:150: multiple definition of `_scobject::get_perimeter()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:150: first defined here
sc_game.o: In function `_scobject::get_type()':
/home/george/SpaceCarnage/040107/sc_object.h:151: multiple definition of `_scobject::get_type()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:151: first defined here
sc_game.o: In function `_scobject::get_dir_x()':
/home/george/SpaceCarnage/040107/sc_object.h:152: multiple definition of `_scobject::get_dir_x()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:152: first defined here
sc_game.o: In function `_scobject::get_dir_y()':
/home/george/SpaceCarnage/040107/sc_object.h:153: multiple definition of `_scobject::get_dir_y()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:153: first defined here
sc_game.o: In function `_scobject::get_collision()':
/home/george/SpaceCarnage/040107/sc_object.h:154: multiple definition of `_scobject::get_collision()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:154: first defined here
sc_game.o: In function `_scobject::move()':
/home/george/SpaceCarnage/040107/sc_object.h:158: multiple definition of `_scobject::move()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_object.h:158: first defined here
sc_game.o: In function `_scobstacle::_scobstacle()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:91: multiple definition of `_scobstacle::_scobstacle()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:91: first defined here
sc_game.o: In function `_scobstacle':
/home/george/SpaceCarnage/040107/sc_obstacle.h:91: multiple definition of `_scobstacle::_scobstacle()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:91: first defined here
sc_game.o: In function `_scobstacle::chg_blitSurface(SDL_Surface*)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:100: multiple definition of `_scobstacle::chg_blitSurface(SDL_Surface*)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:100: first defined here
sc_game.o: In function `_scobstacle::chg_danger(bool)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:101: multiple definition of `_scobstacle::chg_danger(bool)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:101: first defined here
sc_game.o: In function `_scobstacle::chg_attack(int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:102: multiple definition of `_scobstacle::chg_attack(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:102: first defined here
sc_game.o: In function `_scobstacle::chg_hp(int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:103: multiple definition of `_scobstacle::chg_hp(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:103: first defined here
sc_game.o: In function `_scobstacle::chg_addImageBack(int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:104: multiple definition of `_scobstacle::chg_addImageBack(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:104: first defined here
sc_game.o: In function `_scobstacle::chg_rmImageBack(int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:105: multiple definition of `_scobstacle::chg_rmImageBack(int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:105: first defined here
sc_game.o: In function `_scobstacle::chg_loadImage(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:108: multiple definition of `_scobstacle::chg_loadImage(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:108: first defined here
sc_game.o: In function `_scobstacle::chg_imageAddTime()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:117: multiple definition of `_scobstacle::chg_imageAddTime()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:117: first defined here
sc_game.o: In function `_scobstacle::chg_imageTimeGoal(unsigned int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:118: multiple definition of `_scobstacle::chg_imageTimeGoal(unsigned int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:118: first defined here
sc_game.o: In function `_scobstacle::chg_imageSpecifyTimeToAdd(unsigned int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:119: multiple definition of `_scobstacle::chg_imageSpecifyTimeToAdd(unsigned int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:119: first defined here
sc_game.o: In function `_scobstacle::show()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:122: multiple definition of `_scobstacle::show()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:122: first defined here
sc_game.o: In function `_scobstacle::get_images()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:135: multiple definition of `_scobstacle::get_images()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:135: first defined here
sc_game.o: In function `_scobstacle::get_blitSurface()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:136: multiple definition of `_scobstacle::get_blitSurface()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:136: first defined here
sc_game.o: In function `_scobstacle::get_dangerous()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:137: multiple definition of `_scobstacle::get_dangerous()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:137: first defined here
sc_game.o: In function `_scobstacle::get_attack()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:138: multiple definition of `_scobstacle::get_attack()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:138: first defined here
sc_game.o: In function `_scobstacle::get_hp()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:139: multiple definition of `_scobstacle::get_hp()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:139: first defined here
/home/george/SpaceCarnage/040107/sc_obstacle.h:108: multiple definition of `_scobstacle::chg_loadImage(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:108: first defined here
sc_game.o: In function `_scobstacle::chg_imageAddTime()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:117: multiple definition of `_scobstacle::chg_imageAddTime()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:117: first defined here
sc_game.o: In function `_scobstacle::chg_imageTimeGoal(unsigned int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:118: multiple definition of `_scobstacle::chg_imageTimeGoal(unsigned int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:118: first defined here
sc_game.o: In function `_scobstacle::chg_imageSpecifyTimeToAdd(unsigned int)':
/home/george/SpaceCarnage/040107/sc_obstacle.h:119: multiple definition of `_scobstacle::chg_imageSpecifyTimeToAdd(unsigned int)'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:119: first defined here
sc_game.o: In function `_scobstacle::show()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:122: multiple definition of `_scobstacle::show()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:122: first defined here
sc_game.o: In function `_scobstacle::get_images()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:135: multiple definition of `_scobstacle::get_images()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:135: first defined here
sc_game.o: In function `_scobstacle::get_blitSurface()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:136: multiple definition of `_scobstacle::get_blitSurface()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:136: first defined here
sc_game.o: In function `_scobstacle::get_dangerous()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:137: multiple definition of `_scobstacle::get_dangerous()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:137: first defined here
sc_game.o: In function `_scobstacle::get_attack()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:138: multiple definition of `_scobstacle::get_attack()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:138: first defined here
sc_game.o: In function `_scobstacle::get_hp()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:139: multiple definition of `_scobstacle::get_hp()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:139: first defined here
sc_game.o: In function `_scobstacle::get_currentImage()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:140: multiple definition of `_scobstacle::get_currentImage()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:140: first defined here
sc_game.o: In function `_scobstacle::get_imageTimeGoal()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:141: multiple definition of `_scobstacle::get_imageTimeGoal()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:141: first defined here
sc_game.o: In function `_scobstacle::get_imageTimeToAdd()':
/home/george/SpaceCarnage/040107/sc_obstacle.h:142: multiple definition of `_scobstacle::get_imageTimeToAdd()'
sc_functions.o:/home/george/SpaceCarnage/040107/sc_obstacle.h:142: first defined here
sc_functions.o:(.gnu.linkonce.r._ZTV11_scobstacle+0x10): undefined reference to `_scobstacle::chg_images()'
collect2: ld returned 1 exit status
make: *** [sc_game] Error 1

Any ideas?

tuxdev 05-01-2007 08:40 PM

You've got a separate declaration and definition in the same file. You need to either declare and define in the same place, or move the definitions to a .c file.

graemef 05-02-2007 12:18 AM

The only time you want the definition in the .h file is when the function is an inline or template function

RHLinuxGUY 05-02-2007 02:49 AM

I separated my header files so one is just the class declaration, and another header that contains the definitions. Here is what I get when I compile the program:

sc_object_def.h:5: error: virtual outside class declaration

//... and the same error for every virtual function.

This is a problem I had before. I can only get virtual functions working ({} ones, not ones with a = 0; ), ONLY if I make them inline. How can I fix this?

tuxdev 05-02-2007 08:24 AM

sc_object_def.h should be called sc_object.c. It shouldn't be #included anywhere, and the .h extension implies that the file should be #included.

Also, you can't use the keyword virtual when you define a method. Just take it out, the compiler knows the rest (you shouldn't use the keyword static either).

dmail 05-02-2007 08:49 AM

I don't have time at the moment to go into more detail but here is a couple of things I would just like to say.
Two of you headers seem to be the same.
Don't prefix with an underscore, these are reserved.
inline functions which are defined in a header and outside of the class body.
And my pet hate, get and set (or in this case chg_*) functions, what's the point of these?
Have a look at following as an example
Code:

struct
{
int type();
void type(int set_type);
};

No get and set required just function overloads.

In _scgame constructor you setup SDL and create the window yet don't check any return values, I really think you should and if it fails throw and exception.

Make constant functions const and look at returning references and refs to const.

RHLinuxGUY 05-04-2007 01:54 AM

Sorry for the long delay, I was hunting down information with the information I received here.

I now know that virtual functions are defined virtual functions as a declaration in the class.
(e.g.
Code:

virtual type function ();
)

And defined without the virtual part in an external source file, with reference to a class. (inherited or not) (.cpp).
(e.g.
Code:

type theClass::function () { /* ... */ }
)

And never (unless your program does not need to be this elaborate) use inline functions in classes, where it is included in multiple files.
(e.g.

The Right Way:

Class Header File
Code:

#ifndef myClass_h_
#define myClass_h_

class myClass
{
public:
 // myVirtFunc is just declared, not defined.
 virtual void myVirtFunc ();
        void myFunc    ();
};

#endif

Source File:
Code:

#include //... the needed headers

#include "myClass.h"

void myClass::myVirtFunc () { /* ... */ }
void myClass::myFunc    () { /* ... */ }

void mySubClass::myVirtFunc () { /* ... */ }

The Way I Did It (Wrong Way):
Code:

#ifndef myClass_h_
#define myClass_h_

class myClass
{
public:
 // myVirtFunc () gets defined more than once, if added to several source files.
 virtual void myVirtFunc () { /* ... */ }
        void myFunc    ();
};

#endif

)

I know this may have already been known by the CPP gurus in this forum, but I like detailed explanation of what went wrong (posted by others) and how it was fixed, with example code (that I just shown). Just so if another me happens to fall into this same problem, and needs details. :)


Thanks everyone.

dmail 05-04-2007 06:33 AM

RHLinuxGUY, who told you the following?
Quote:

And never (unless your program does not need to be this elaborate) use inline functions in classes, where it is included in multiple files.
The following is fine, its just an inline function.
Code:

#ifndef myClass_h_
#define myClass_h_

class myClass
{
public:
 // myVirtFunc () gets defined more than once, if added to several source files.
 virtual void myVirtFunc () { /* ... */ }
        void myFunc    ();
};

#endif

I think you will find this was more your problem
Quote:

inline functions which are defined in a header and outside of the class body.
Ie all of these:
Code:

// insert data functions
void _scobject::chg_boxArea  ( int boxArea_arg )    { boxArea = boxArea_arg; }
void _scobject::chg_clipImage ( bool clipImage_arg ) { clipImage = clipImage_arg; }
void _scobject::chg_viewable  ( bool viewable_arg )  { viewable = viewable_arg; }
void _scobject::chg_position  ( Sint16 x_arg,
                                Sint16 y_arg )      { xywh.x = x_arg; xywh.y = y_arg; }
void _scobject::chg_perimeter ( Sint16 w_arg,
                                Sint16 h_arg )      { xywh.w = w_arg; xywh.h = h_arg; }
void _scobject::chg_type      ( int type_arg )      { type = type_arg; }
void _scobject::chg_dir_x    ( int move_x_arg )    { move_x = move_x_arg; }
void _scobject::chg_dir_y    ( int move_y_arg )    { move_y = move_y_arg; }
void _scobject::chg_collision ( bool collision_arg ) { collision = collision_arg; }

 // retrieve data functions
int      _scobject::get_boxArea  () { return boxArea; }
bool    _scobject::get_clipImage () { return clipImage; }
bool    _scobject::get_viewable  () { return viewable; }
SDL_Rect _scobject::get_position  () { return xywh; }
SDL_Rect _scobject::get_perimeter () { return xywh; }
int      _scobject::get_type      () { return type; }
int      _scobject::get_dir_x    () { return move_x; }
int      _scobject::get_dir_y    () { return move_y; }
bool    _scobject::get_collision () { return collision; }


http://www.parashift.com/c++-faq-lit...s.html#faq-9.8

RHLinuxGUY 05-04-2007 10:16 AM

Quote:

Originally Posted by dmail
RHLinuxGUY, who told you the following?


The following is fine, its just an inline function.
Code:

#ifndef myClass_h_
#define myClass_h_

class myClass
{
public:
 // myVirtFunc () gets defined more than once, if added to several source files.
 virtual void myVirtFunc () { /* ... */ }
        void myFunc    ();
};

#endif


Oops, my mistake, I don't remember what I was thinking when I was writing that post..

Quote:

Originally Posted by dmail
I think you will find this was more your problem


Ie all of these:
Code:

// insert data functions
void _scobject::chg_boxArea  ( int boxArea_arg )    { boxArea = boxArea_arg; }
void _scobject::chg_clipImage ( bool clipImage_arg ) { clipImage = clipImage_arg; }
void _scobject::chg_viewable  ( bool viewable_arg )  { viewable = viewable_arg; }
void _scobject::chg_position  ( Sint16 x_arg,
                                Sint16 y_arg )      { xywh.x = x_arg; xywh.y = y_arg; }
void _scobject::chg_perimeter ( Sint16 w_arg,
                                Sint16 h_arg )      { xywh.w = w_arg; xywh.h = h_arg; }
void _scobject::chg_type      ( int type_arg )      { type = type_arg; }
void _scobject::chg_dir_x    ( int move_x_arg )    { move_x = move_x_arg; }
void _scobject::chg_dir_y    ( int move_y_arg )    { move_y = move_y_arg; }
void _scobject::chg_collision ( bool collision_arg ) { collision = collision_arg; }

 // retrieve data functions
int      _scobject::get_boxArea  () { return boxArea; }
bool    _scobject::get_clipImage () { return clipImage; }
bool    _scobject::get_viewable  () { return viewable; }
SDL_Rect _scobject::get_position  () { return xywh; }
SDL_Rect _scobject::get_perimeter () { return xywh; }
int      _scobject::get_type      () { return type; }
int      _scobject::get_dir_x    () { return move_x; }
int      _scobject::get_dir_y    () { return move_y; }
bool    _scobject::get_collision () { return collision; }


That was part of the problem too. I failed to mention vtable problems after I fixed my former issue.


All times are GMT -5. The time now is 05:52 AM.