Thread: Getting a "multiple definition of <classname>" error.

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Getting a "multiple definition of <classname>" error.

    I have a file that includes another class and when I try to make an object of that class I get the error "multiple definition of..." and the name of that class. Why am I getting these errors. I have header guards and there is only one instance of the class, so why do I get multiple definition errors.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Who knows. Let's see the code.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Code:
    #ifndef SCENES_H_INCLUDED
    #define SCENES_H_INCLUDED
    
    #include <GL/glew.h>
    #include <GL/CTargaImage.h>
    #include "TerragenFile.h"
    
    namespace Box
    {
        void setupBoxScene();
        void renderBox();
    }
    
    
    namespace Terrain
    {
        TerragenFile ter;
        bool setupTerrain();
        void renderTerrain();
    }
    
    #endif // SCENES_H_INCLUDED
    Code:
    #include "scenes.h"
    void Terrain::renderTerrain()
    {
    
    }
    
    bool Terrain::setupTerrain()
    {
        return true;
    }
    I haven't even implemented it and it gives me this. Plus the error points to some file called "locale_facets.tcc"

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I solved it by moving the instantiation of the class into the cpp file. I need to brush up a little on the namespace a little, I was using it on instinct and examples.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by indigo0086 View Post
    I solved it by moving the instantiation of the class into the cpp file. I need to brush up a little on the namespace a little, I was using it on instinct and examples.
    The only things you should EVER put in a header file are DECLARATIONS. You can declare a class, but don't instantiate it. As you have discovered

    So, question: why did you do it that way in the first place? Do you want the "ter" object to be accessible from multiple source files? Then you should declare it as "extern." And some (one, and only one) source file should define it as a real variable.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I thought namespace had "classlike" functioality, have a namespace have a 'member' so to speak. Now I get why it can't be that way. I was just green with namespaces. I'll use extern.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM