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
Multiple Definition (C++)
Moderator: Coders of Rage
Re: Multiple Definition (C++)
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
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++)
Thanks! This fixed the issue. Would it be okay for me to move the "using namespace std" into the cpp?
Re: Multiple Definition (C++)
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:
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.
Code: Select all
void function() {
using namespace std;
//blah blah
}
- LeonBlade
- 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++)
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
Code: Select all
using namespace std;
You could just use this syntax if you like...
Code: Select all
std::cout << "hello world";
There's no place like ~/
- short
- 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++)
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
link: https://github.com/bjadamson
- Falco Girgis
- 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++)
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.