Public Group

Converting an integer to a string

This topic is 4242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all, I need to convert an integer to a string. The member "level" of the class "bot" is a list[20]. This is my code:
string number = "number=" + itos(bot[numero].levels[j].size());

template <class X>
string itos(const X i)	// convert int to string
{
std::stringstream s;
s << i;
return s.str();
}


template <class X>
std::string itos(const X);


The problem is I get some error LNK2019: "external symbols not resolved". I know I can use itoa to, but it seems using a function like this is easier. Thanks for your help.

Share on other sites
You can't do that with templates without support for the export keyword. You'll either need to include the definition in the header file, or include the .cpp file into the header.

[Edited by - Washu on February 14, 2007 1:39:20 PM]

Share on other sites
Quote:
 Original post by WashuYou can't do that with templates without support for the extern keyword. You'll either need to include the definition in the header file, or include the .cpp file implementation file, which should be renamed to something not ending with .cpp into the header.

Fixed. I would use something along the lines of .impl.hpp, since your IDE is probably already configured to treat .hpp files as C++ code. But #including .cpp files is just asking for more linker troubles -- namely, duplicate symbol errors the moment you define a non-template function/variable in the .cpp file, as people usually expect to be able to do with .cpp files.

...if it wasn't already obvious, you'll also have to put any of the aforementioned non-template function/variables into a different file, for the exact aforementioned reasons :).

Share on other sites
Thank you very much. The problem has been solved.

Thanks.

Share on other sites
Isn't there an export keyword for doing this kind of job? Although, I am not sure if the keyword is compiler specific.

Share on other sites
Quote:
 Isn't there an export keyword for doing this kind of job? Although, I am not sure if the keyword is compiler specific

Quote:
 You can't do that with templates without support for the export keyword. You'll either need to include the definition in the header file, or include the .cpp file into the header.

the problem is its only supported by one compiler (EDG iirc)

[Edited by - Washu on February 14, 2007 1:35:40 PM]

Share on other sites
Maybe I have just written so many toStr() functions, but now, without including ANYTHING (maybe I have a default include?)

I do toStr( and then VC2005EE shows that its a method and I do it and bam. Maybe I',m just high =(

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• Forum Statistics

• Total Topics
632660
• Total Posts
3007693
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!