Multiple Definition (C++)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Multiple Definition (C++)

Post by ajtgarber »

In my header file here (Utils.h), I've been getting a multiple definition error for every method declared within. I have #include guards, and when I switch everything to inline it compiles
just fine. Though I don't want to have every function be inline.
http://pastebin.com/index/d7BZUtih

Since I've added the loadFont, and the drawText functions it has been giving me this error.

Language: C++
Compiler: gcc 4.4
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: Multiple Definition (C++)

Post by bnpph »

Code: Select all

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <iostream>
#include <fstream>
#include <string>

#include "Map.h"
#include "Tile.h"

using namespace std;

#ifndef UTILS_H
#define UTILS_H
First off, put #includes inside your guards. This will improve compile time.
Second, don't use "using namespace std" inside of headers. This causes namespace scope problems.

Anyways, put your declarations in your headers, and then move the function implementations to their own .cpp file. This is the proper way to do so, and should fix the "multiple definition" error you are getting.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Multiple Definition (C++)

Post by ajtgarber »

Thanks! This fixed the issue. Would it be okay for me to move the "using namespace std" into the cpp?
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: Multiple Definition (C++)

Post by bnpph »

It is safe to put it in the cpp, but I generally avoid that too - I prefer to put it in every function that needs it:

Code: Select all

void function() {
  using namespace std;
  //blah blah
}
This makes copy+pasting easier, and specifically states which functions require the std. If you have inline functions in the .h file, you also should put "using std" inside the functions.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Multiple Definition (C++)

Post by LeonBlade »

I never use the...

Code: Select all

using namespace std;
...call because I just don't like it :P

You could just use this syntax if you like...

Code: Select all

std::cout << "hello world";
...this can get annoying if you have a lot of STD calls, I guess I just like using the C libraries over the C++ ones ;)
There's no place like ~/
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Multiple Definition (C++)

Post by short »

It's a balance, which is more important the amount of typing you have to do as a programmer vs readability of your code. std::cout is more "clear" than using cout "using namespace std" because it tells whomever is reading your code where the cout function comes from, the std namespace, right there when they are reading that line of code. They don't have to go look elsewhere if they didn't know. However that can usually be ignored with the std, and most namespaces if your only using a couple "using" statements. I just wanted to throw out my complete academia perspective they taught us about readability, I love using "using" statements when I program.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Multiple Definition (C++)

Post by Falco Girgis »

I'll use a namespace sometimes in a .CPP file, but if I'm ever using functionality from two namespaces, I ALWAYS use the namespace prefix... it gets really confusing/shitty to look at otherwise.
Post Reply