It will compile fine, but if I declare the body of doSomething() outside of the class then I get link errors
"error LNK2019: unresolved external symbol "
I've tested this with GCC and it compiles fine, I've heard Visual Studio 6.0 can't do this but nothing about Visual Studio 2008.
The above works fine for me (there is a slight edit). Compiles without error in VS 2010. I don't have 2008 to test with but I've created templates in it before with no problem. I'm thinking your linker error is unrelated to templates.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
The above works fine for me (there is a slight edit). Compiles without error in VS 2010. I don't have 2008 to test with but I've created templates in it before with no problem. I'm thinking your linker error is unrelated to templates.
I've been going over my code none stop for over an hour now, it's to do with templates. I guess it's time to upgrade though, has to happen sometime.
Thanks for your help.
EDIT:
Actually, it seems like it's only when the function is being called. I guess this means it doesn't have to do with templates specifically BUT I still have no idea what's wrong with it
Okay now I'm a little more awake I've dug up what's causing the problem here.
I was using a old(ish) version of GCC which is probably why it compiled. And all the examples posted so far were also correct but the functions need or at least should be inlined inside the header. You can't declare the bodies in a source file.
As far as I know there isn't many decent ways around this, but if anyone wants to suggest one then feel free.
N64vSNES wrote:And all the examples posted so far were also correct but the functions need or at least should be inlined inside the header. You can't declare the bodies in a source file.
Not sure what you mean by this. The code I posted was meant to all go in "MyClass.h". Were you trying to define the function in a .cpp file? If you really want to separate it out you can, but not in a .cpp file (due to the way VS handles that extension).
N64vSNES wrote:And all the examples posted so far were also correct but the functions need or at least should be inlined inside the header. You can't declare the bodies in a source file.
Not sure what you mean by this. The code I posted was meant to all go in "MyClass.h". Were you trying to define the function in a .cpp file? If you really want to separate it out you can, but not in a .cpp file (due to the way VS handles that extension).
Yeah, I always hate declaring bodies in header files, it seems untidy to me. It's rare I do it. I'm sure I've used templates like this in VS before, Ah well.
bnpph wrote:You can do it in source files if you know the types you are using. Also if you use a non-standard extension.
Yeah I read about this, but it sounds nasty restricting the types that can be used like that. I guess if it's only intended for primitive data types then I suppose it's fine though.