Page 1 of 1
Multiple Definition (C++)
Posted: Sat May 14, 2011 2:10 pm
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
Re: Multiple Definition (C++)
Posted: Sat May 14, 2011 2:34 pm
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.
Re: Multiple Definition (C++)
Posted: Sat May 14, 2011 8:03 pm
by ajtgarber
Thanks! This fixed the issue. Would it be okay for me to move the "using namespace std" into the cpp?
Re: Multiple Definition (C++)
Posted: Sat May 14, 2011 10:02 pm
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.
Re: Multiple Definition (C++)
Posted: Mon May 16, 2011 6:07 am
by LeonBlade
I never use the...
...call because I just don't like it :P
You could just use this syntax if you like...
...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
Re: Multiple Definition (C++)
Posted: Mon May 16, 2011 11:38 am
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.
Re: Multiple Definition (C++)
Posted: Mon May 16, 2011 1:20 pm
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.