Errors:
[code]
1>map.obj : error LNK2005: "int __cdecl RandomUtil(int,int)" (?RandomUtil@@YAHHH@Z) already defined in main.obj
1>map.obj : error LNK2005: "int __cdecl Round(double)" (?Round@@YAHN@Z) already defined in main.obj
1> Creating library C:\Users\Quincy\Documents\Visual Studio 2008\Projects\Test\Debug\Test.lib and object C:\Users\Quincy\Documents\Visual Studio 2008\Projects\Test\Debug\Test.exp
1>C:\Users\Quincy\Documents\Visual Studio 2008\Projects\Test\Debug\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
[/code]
Code :
[cpp]
//main.cpp
#include <iostream>
#include <time.h>
#include <string>
#include "map.hpp"
int main()
{
std::string Input;
int Height,Width,quit;
quit=1;
srand(time(NULL));
std::cout << "Map Generator !" << std::endl << std::endl;
std::cout << "Input the number height and lenght" << std::endl;
std::cout << "Height : ";
std::cin >> Height;
std::cout << std::endl << "Width : ";
std::cin >> Width;
Map map (Width, Height);
while(quit!=0)
{
}
return 1;
}
//map.cpp
#include "map.hpp"
Map::Map(int W, int H)
{
Width = W;
Height = H;
map = new (std::nothrow) int[Width*Height];
for (int n=0; n<Height; n++)
{
for (int m=0; m<Width; m++)
{
map[n*Width+m] = 0;
}
}
}
void Map::Randomize()
{
int HillPos [5];
int size = Width*Height;
int NumHills = Round(size/25);
for (int n=0; n<=NumHills; n++)
{
HillPos[n] = RandomUtil(0,size);
}
}
void Map::SetHeight(int x,int y,int height)
{
map[((y-1)*Width+x)-1] = height;
}
void Map::Showmap()
{
std::cout << std::endl;
for (int n=0; n<Height; n++)
{
for (int m=0; m<Width; m++)
{
std::cout << map[n*Width+m];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
//map.hpp
#ifndef MAP
#define MAP
#include <iostream>
#include <new>
#include "utils.hpp"
class Map
{
private:
int Width,Height;
int * map;
public:
Map(int W,int H);
void SetHeight(int x,int y,int height);
void Randomize();
void Showmap();
};
#endif
[/cpp]
I had to type/paste this very quickly but can anyone find the problem ?
Thanks in advance
The problem is in your utils.hpp, im guessing you have functions (specifically randomUtil and round) in the header which arn't inlined.
Change the top of your map.hpp so that the includes are above the header-included-check (or whatever they are called), like this:
[cpp]
#include <iostream>
#include <new>
#include "utils.hpp"
#ifndef MAP
#define MAP
[/cpp]
Then make sure utils.hpp has the same sort of system.
[b]Edit:[/b] You should also use a different naming convention for your header-included-check definitions (e.g: MAP_HPP_INCLUDED) to prevent collisions.
[QUOTE=Deco Da Man;18108938]Change the top of your map.hpp so that the includes are above the header-included-check (or whatever they are called), like this:
[cpp]#ifndef MAP
#define MAP
#include <iostream>
#include <new>
#include "utils.hpp"
[/cpp]
Then make sure utils.hpp has the same sort of system.
[b]Edit:[/b] You should also use a different naming convention for your header-included-check definitions (e.g: MAP_HPP_INCLUDED) to prevent collisions.[/QUOTE]
You're missing an #endif...
[QUOTE=jA_cOp;18109004]You're missing an #endif...[/QUOTE]
I was only quoting the top part of the file...
[b]Edit:[/b] Fuck, I had forgotten to make the change I was telling him to do...
What are you guys talking about, his include guards are completely fine (unless I'm blind). But yea they should be named a bit less ambiguously.
The problem is with his utils.hpp which either has non-inline functions in the header or no include guards.
[editline]05:48PM[/editline]
[QUOTE=Deco Da Man;18109108]I was only quoting the top part of the file...
[b]Edit:[/b] Fuck, I had forgotten to make the change I was telling him to do...[/QUOTE]
WOAH
That is the [B]incorrect[/B] way!
[QUOTE=Deco Da Man;18109108]I was only quoting the top part of the file...[/QUOTE]
[QUOTE=Deco Da Man;18108938]Change the top of your map.hpp[/QUOTE]
Half the solution, basically?
[QUOTE=r4nk_;18109119]WOAH
That is the [B]incorrect[/B] way![/QUOTE]
It works fine for me.
I had the same error, until I changed my headers to how I stated above.
[QUOTE=Deco Da Man;18109168]It works fine for me.
I had the same error, until I changed my headers to how I stated above.[/QUOTE]
Way to avoid the real problem. Read my post if you want to know the real problem.
Please explain to me how making the functions in util.hpp inline will help.
I'm assuming you mean:
[cpp]
inline int SomeFunction() { whatever; }
[/cpp]
If you mean placing the function in a header guard, then we are actually saying the same thing:
[QUOTE=Deco Da Man;18108938]Then make sure utils.hpp has the same sort of system.[/QUOTE]
Except you have the wrong terminology.
[QUOTE=Deco Da Man;18109252]Please explain to me how making the functions in util.hpp inline will help.[/QUOTE]
Ok I will explain it. If you include the header in more than 1 other file, non-inlined functions will have [B]multiple definitions[/B]. Same goes for non-static, non-extern variables:
[cpp]
#ifndef FILE_H
#define FILE_H
int x;
#endif[/cpp]
Will cause multiple definitions of x, if you include the file more than once.
[QUOTE=Deco Da Man;18109252]
If you mean placing the function in a header guard, then we are actually saying the same thing:
Except you have the wrong terminology.[/QUOTE]
If you are refering to me saying "include" guard, they mean the exact same thing.
If you are refering to the way I used the include guards, you are using them incorrectly. This is the correct way:
[cpp]
#ifndef FILE_H
#define FILE_H
//INCLUDES HERE
//EOF
#endif
[/cpp]
He means non inlined functions defined in the header, not declared.
-snip-
[editline]03:56AM[/editline]
broke my automerge
fuck dat mufin
[QUOTE=r4nk_;18109410]
[cpp]
#ifndef UTIL_HPP_INCLUDED
#define UTIL_HPP_INCLUDED
const char* the_util_func()
{
return "Hello world!";
}
#endif // UTIL_HPP_INCLUDED
[/cpp][/QUOTE]
That's not inlining, that's declaring.
[b]Edit:[/b] After doing a bit of googling, I've found that inline doesn't just mean a keyword that can be used with functions to do a certain compiler optimisation. It can also mean declaring a function in a header.
I apologise for causing confusion.
[quote=deco da man;18109485]that's not inlining, that's declaring.
[b]edit:[/b] after doing a bit of googling, i've found that inline doesn't just mean a keyword that can be used with functions to do a certain compiler optimisation. It can also mean declaring a function in a header.
I apologise for causing confusion.[/quote]
NO SHIT ITS NOT INLINING
ITS DEFINING
ITS NOT EVEN JUST DECLARING
IN THE HEADER
THATS WHAT THIS WHOLE THREAD IS ABOUT
where have you been!?
[QUOTE=r4nk_;18108524]The problem is in your utils.hpp, im guessing you have functions (specifically randomUtil and round) in the header [b]which arn't inlined.[/b][/QUOTE]
Crazy man, you are.
Yeah what the fuck is wrong with that?
If it isn't an inline function there will be multiple definitions
I have no idea what the hell is happening after reading through the thread.
All I have to say is define your functions in a cpp file and declare them in a header which is included by the cpp which defines it.
Problem solved.
Whats with all the inline crap?
[QUOTE=Jallen;18109540]I have no idea what the hell is happening after reading through the thread.
All I have to say is define your functions in a cpp file and declare them in a header which is included by the cpp which defines it.
Problem solved.
Whats with all the inline crap?[/QUOTE]
The OP is having a multiple definitions error
Defining functions which arn't inlined inside a header will create multiple definitions if the header is included more than once.
The header in question is a utility header he made, which leads me to believe he is writing the small functions inside the header, and has forgotten to mark them as inline.
/thread
I didn't know making a function inline fixed this.
Rate me dumb, several times over.
[QUOTE=r4nk_;18109551]The OP is having a multiple definitions error
Defining functions which arn't inlined inside a header will create multiple definitions if the header is included more than once.
The header in question is a utility header he made, which leads me to believe he is writing the small functions inside the header, and has forgotten to mark them as inline.
/thread[/QUOTE]
Right, yeah.
[B]GOOD[/B]
[code]
#ifndef lewl_h
#define lewl_h
inline void rofl()
{
cout<<"MUDDDDKIPZZZZ\n";
}
#endif[/code]
[B]BAD[/B]
[code]
#ifndef lewl_h
#define lewl_h
void rofl()
{
cout<<"MUDDDDKIPZZZZ\n";
}
#endif[/code]
[B]GOOD[/B]
[code]
#ifndef lewl_h
#define lewl_h
void rofl();
#endif[/code][code]#include "lewl.h"
void rofl()
{
cout<<"MUDDDDKIPZZZZ\n";
}[/code]
[QUOTE=Deco Da Man;18109562]_r4nk.
By the usage of the word 'inline' by a vastly large portion of C++ coders (including the [url=http://www.parashift.com/c++-faq-lite/inline-functions.html]C++ FAQ Lite[/url]), I had to assume you meant:
[cpp]
inline int whatever() {
whatever;
}
[/cpp]
[b]Get your terminology right.[/b]
This is the exact same question I have.[/QUOTE]
Jesus christ
You are telling me to get my terminology right:
[QUOTE=Deco Da Man;18109485]That's not inlining, that's declaring.
[b]Edit:[/b] After doing a bit of googling, I've found that inline doesn't just mean a keyword that can be used with functions to do a certain compiler optimisation. It can also mean declaring a function in a header.
I apologise for causing confusion.[/QUOTE]
That's DEFINING not DECLARING
And this:
[cpp]
inline int whatever() {
whatever;
}
[/cpp]
IS what im talking about
[editline]07:16PM[/editline]
Ok Deco I think I might see where you are getting confused.
When I made this post:
[QUOTE=r4nk_;18109410][cpp]
#ifndef UTIL_HPP_INCLUDED
#define UTIL_HPP_INCLUDED
const char* the_util_func();
#endif // UTIL_HPP_INCLUDED
[/cpp]
:facepalm:
We are talking about THIS:
[cpp]
#ifndef UTIL_HPP_INCLUDED
#define UTIL_HPP_INCLUDED
const char* the_util_func()
{
return "Hello world!";
}
#endif // UTIL_HPP_INCLUDED
[/cpp][/QUOTE]
I meant, the second example will NOT work because it is not an inlined function. I think you think that an inlined function is one thats simply put in a header with no need for the inlined keyword. Whereas I am actually talking about this being the solution:
[cpp]
#ifndef UTIL_HPP_INCLUDED
#define UTIL_HPP_INCLUDED
inline const char* the_util_func()
{
return "Hello world!";
}
#endif // UTIL_HPP_INCLUDED
[/cpp]
If that's not where the confusion is coming from, then I don't know what you are smoking but hook me up with some
Yep, that's it.
Sorry for causing confusion :/
[QUOTE=Deco Da Man;18109626]Yep, that's it.
Sorry for causing confusion :/[/QUOTE]
[cpp]
/*Declaration*/
extern int a;
void func(void);
class A;
/*Definition*/
int a;
void func(){}
class A{};
[/cpp]
Not knowing the difference will cause you a lot of frustration in C and C++.
[QUOTE=jA_cOp;18109738][cpp]
/*Declaration*/
extern int a;
void func(void);
class A;
/*Definition*/
int a;
void func(){}
class A{};
[/cpp]
Not knowing the difference will cause you a lot of frustration in C and C++.[/QUOTE]
Thanks.
I always get the two confused.
Anyway, now that we've sorted out my idiocy, do you understand the problem, quincy18?
Sorry, you need to Log In to post a reply to this thread.