How to read about error expecially about unresolved errors?

Oct 24, 2014 at 2:49pm
If I have this error

Error 1 error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl toString<unsigned int>(unsigned int const &)" (??$toString@I@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABI@Z) referenced in function "private: void __thiscall Game::updateStatistics(class sf::Time)"


How can i read it? There's no line numbers and its looks really messy. I cant really understand it apart from the "unresolved external"
Oct 24, 2014 at 2:59pm
Is this the only warning or error being reported?

The problem appears to be happening in Game::updateStatistics(class sf::Time). It appears that the compiler can't find something to do with toString.

Oct 24, 2014 at 3:02pm
Yes thats the only one.

what is this cdec1?

and this too

(??$toString@I@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABI@Z ?

this is the template where it gets the toString()
1
2
template <typename T>
std::string toString(const T& value);


if thattemplate is causing this error, why does the compiler show this
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > ?

what does the compiler mean by all of those unrelated words showing up in the error? Makes the debugging harder for me :(
Last edited on Oct 24, 2014 at 3:04pm
Oct 24, 2014 at 3:20pm

what does the compiler mean by all of those unrelated words showing up in the error?

This is not the compiler talking, since this is a linker error the error is actually being generated by the linker. Your linker talking about the std::string class, which is derived from st::basic_string<>.

what is this cdec1

That's cdecl (lower case L). And that is one of the many different calling conventions used by the compiler and linker.

this is the template where it gets the toString()

And where is the actual implementation, and remember with templates the implementation and definition must be in the same compilation unit.

Oct 24, 2014 at 3:27pm
This is my .h file
1
2
3
#include <sstream>
template <typename T>
std::string toString(const T& value);


this is the implementation. It is a .inl file
1
2
3
4
5
6
7
8
#include "StringHelpers.h"
template <typename T>
std::string toString(const T& value)
{
	std::stringstream stream;
	stream << value;
	return stream.str();
}


This is my main. Where i call the template class
1
2
3
4
5
6
7
#include "StringHelpers.h"
int main(){

mStatisticsText.setString(
				"Frames / Second = " + toString(mStatisticsNumFrames) + "\n" +
				"Time / Update = " + toString(mStatisticsUpdateTime.asMicroseconds() / mStatisticsNumFrames) + "us");
}


The template definition has a declaration on .inl file. So was wondering how and why does compiler cant see the declaration?

Is it because its on .inl file?

same compilation unit.


Sorry i was lost here. What do you mean by same compilation unit?
Last edited on Oct 24, 2014 at 3:28pm
Oct 24, 2014 at 3:37pm
Looks like you're trying to base this off of the code in the game development book.

Templates can be annoying in this regard and C++ doesn't have a solution (yet?) to correctly separate interface from implementation.

Look at https://github.com/LaurentGomila/SFML-Game-Development-Book/blob/master/01_Intro/Include/Book/StringHelpers.inl
"StringHelpers.h[pp]" is not included in the .inl file. The .inl file is included into the StringHelpers header.
See Line 9:
1
2
3
4
5
6
7
8
9
10
11
#ifndef BOOK_STRINGHELPERS_HPP
#define BOOK_STRINGHELPERS_HPP

#include <sstream>
// Since std::to_string doesn't work on MinGW we have to implement
// our own to support all platforms.
template <typename T>
std::string toString(const T& value);
#include <Book/StringHelpers.inl>

#endif // BOOK_STRINGHELPERS_HPP 
Last edited on Oct 24, 2014 at 3:45pm
Oct 24, 2014 at 3:55pm
I see.

Thats really annoying. I hope they add that feature on next c++ version. maybe c++15 :D

I must be very carefull then on using header files and templates.

Thanks.

Topic archived. No new replies allowed.