Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

linking error

Started by Amarth Feb 21, 2005 at 3:07 PM 7 replies 1.8k views
Original Post
Amarth
Amarth
I usually don't post this sort of stuff (I usually don't post :D), but I really don't know how to start on this one... It most probably has to do with function overloading and me not at all understanding it. I have an overloaded inline function, like this:

inline std::string toStr(float x){
  char buff[32];
  sprintf(buff,"%g",x);
  return buff;
}

inline std::string toStr(int x){
  char buff[32];
  sprintf(buff,"%i",x);
  return buff;
}


Don't mind the std::string stuff, it isn't meant to run fast, it's meant to run good, and it does. It worked as it should for a long time, until I realized I only used the int-version. So I try out the float-version, and *wham* link error: error LNK2001: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl toStr(float)" (?toStr@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@Z ) The bodies are written, there are no libraries to include, and nothing of the first few google answers applies. I suspect something about inlines and overloading I didn't work out right... But please tell me. Thanks, -Amarth
Drew_Benton
Drew_Benton
I believe it would have to do with the inlines. Have you tried removing the inline and compiling? I was able to paste that into my current project and it compiled fine, so I am thinking maybe with the usage you have.

If that does not resolve it, you may need to use a different structure of toStr. I know you could make it a template and use sstream to output the input number to a string stream, then return the string.

Here is an example:
template < class T>std::string toStr( T num ){	std::stringstream str;	str << num;	return str.str();}


But you would have to verify that the precision is kept with floats. Just a suggestion.

- Drew

[edit] So Dev-CPP doesn't like my templates so here's an alternative:
#include <cstdlib>#include <iostream>#include <sstream>using namespace std;std::string toStr( int num ){	std::stringstream str;	str << num;	return str.str();}std::string toStr( float num ){	std::stringstream str;	str << num;	return str.str();}int main(int argc, char *argv[]){    cout << toStr( 4.4f );    system("PAUSE");    return EXIT_SUCCESS;}


Try adding inline to those as well and see if you get the same thing.
Amarth
Amarth
After a bit more testing...
It doesn't seem to have anything to do with inlines :). It works when I move the code to the file the float-version is used. So it seems to be some sort of (darn what's the word for this... when you split the project in multiple files)-problem. Strangely, the int-function works past file boundaries, but the float-function doesn't... And reversing order in the .h or .cpp file keeps the same error... So it's not an overloading problem.
IIRC, I did it exactly the same way for my int and my float... So why does one of the two work and the other not?
Confusing me quite a lot...

-Amarth
Drew_Benton
Drew_Benton
Quote:
Original post by Amarth
It works when I move the code to the file the float-version is used.


Well then that's the problem. Inline acts sort of as 'paste' into the .cpp file. If you have the int version there, and it works, it's because it was already compiled into the object code. Now you can use toStr(1.1) with the int definition if I am not mistaken. Now when you go to use the float version in another file, it will not work since the definition of the float function is not in the same scope of the onject file. *I bet* if you used prototypes for the two functions and included them in each of the .cpp files, it would work fine. If that does indeed work, then I think it was because of what I've said. If not, [lol], *ops* [wink].

So try adding "inline std::string toStr(float x);" to the .cpp file that uses the float version and see if that works. Just another suggestion, I do not know if it will work.

- Drew
Amarth
Amarth
Isn't this exactly what .h-files are supposed to do [lol]. Anyways I tried it and nope, still same error... Since I already included my .h :). Any other suggestions?
[EDIT]
shouldn't it complain about double definitions when you have both the .h and a manual definition, or is that once again something out of my reach?
[/EDIT]

I could try out templates, but I once tried to understand it and, well, that didn't work :). Perhaps I should look into them. I'm used to Java, and there I learned overloading as Object Oriented standard, so...

-Amarth
meeshoo
meeshoo
well, if they are both inline, then they must be defined in the same class, or in derivated clasess. so i do not see why you are using them in separate files. try searching MSDN for inline functions overload. it might help.
Amarth
Amarth
Quote:
Original post by Rob Loach
I encountered the same problem about a week ago and come up with this solution. Enjoy.


Templates, if I got the hint right :). I'll start looking into them, then...

Quote:
Original post by meesho
well, if they are both inline, then they must be defined in the same class, or in derivated clasess. so i do not see why you are using them in separate files. try searching MSDN for inline functions overload. it might help.


They're pretty global, as they are used in lots of files... could make a class around them, but it seems a bit pointless as it would be a class without instances.

And don't tell me it doesn't work, since it DOES... for the int one at least. If someone can give me a good explanation of why it doesn't work with the float, I'll be very happy.

But I'll try to solve it with templates, that will be the best I believe...

Thanks all!

-Amarth
Rob Loach
Rob Loach
You'll run into a problem if you try to have a template function declared and defined in different files. You'll have to keep the whole templated function declaration and definition in the same .h file. Like I did here. If you have the function declaration the same as the function definition, it automatically becomes an inline function anyway (to my knowledge).
Rob Loach [Website] [Projects] [

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.