Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++]Weird Behavior with templated functions within classes
#1
I discovered something weird while I was learning C++.
When you create a class with a templated function, you can only use it within the file you created the function (not the prototype).
I was wondering if anyone knew how to fix this, since it is rather annoying.

Example: Here.

If you move the function from "CApp_String.cpp" to "CApp.cpp" it works perfectly fine.

I've only tested it using the MingW compiler on Windows, so I don't know if it is any different with any other compiler.
Age ratings for movies and games (and similar) have never been a good idea.
One can learn a lot from reinventing wheels.
An unsound argument is not the same as an invalid one.
volatile in C++ does not mean thread-safe.
Do not make APIs unnecessarily asynchronous.
Make C++ operator > again
Trump is an idiot.
Reply
Thanks given by:
#2
well since u're declaring as part of the CApp class, you should actually put the definition of the CApp::ToString function in the CApp.cpp file.
but i guess that might be beside the point, seeing that there's already the function declaration in the header file.
(i haven't played around with templates much myself, so i won't be able to answer without testing)

what command do u run to compile the app? if the CApp_String.cpp isn't included, then it shouldn't work.

i haven't tried to compile it myself, but maybe i'll try later.



Azriel~
Reply
Thanks given by:
#3
(10-24-2011, 07:08 AM)Azriel Wrote:  well since u're declaring as part of the CApp class, you should actually put the definition of the CApp::ToString function in the CApp.cpp file.
different
The idea of putting it outside (like within "CApp_String.cpp"), is it is easier to work with. I've never had a problem with non-templated functions.

(10-24-2011, 07:08 AM)Azriel Wrote:  what command do u run to compile the app?
Whatever Code::Blocks uses as default. I've included the Code::Blocks Project file (.cbp).

(10-24-2011, 07:08 AM)Azriel Wrote:  if the CApp_String.cpp isn't included, then it shouldn't work.
It is definitely included. It works perfectly as long as it isn't a templated function. (which it in this case is)
Age ratings for movies and games (and similar) have never been a good idea.
One can learn a lot from reinventing wheels.
An unsound argument is not the same as an invalid one.
volatile in C++ does not mean thread-safe.
Do not make APIs unnecessarily asynchronous.
Make C++ operator > again
Trump is an idiot.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)