Page 1 of 1
Visual C++ 2008 Express And Templates
Posted: Fri Jul 08, 2011 4:21 pm
by N64vSNES
I've come across a strange issue with Visual Studio (2008 Express)
Basically if I declare template like so:
Code: Select all
template <class T>
class myClass {
public:
myClass() { }
~myClass() { }
void doSomething(T *obj)
{
// Whatever
}
private:
};
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.
Any suggestions?
Thanks.
Re: Visual C++ 2008 Express And Templates
Posted: Fri Jul 08, 2011 4:24 pm
by dandymcgee
Code: Select all
template <class T>
class myClass {
public:
myClass() { }
~myClass() { }
doSomething(T *obj);
};
template <class T>
void myClass<T>::doSomething(T *obj)
{
// Whatever
}
This should work in VS.
Re: Visual C++ 2008 Express And Templates
Posted: Fri Jul 08, 2011 4:29 pm
by N64vSNES
dandymcgee wrote:Code: Select all
template <class T>
class myClass {
public:
myClass() { }
~myClass() { }
doSomething(T *obj);
};
template <class T>
void myClass<T>::doSomething(T *obj)
{
// Whatever
}
This should work in VS.
it should work yeah, but then I get:
"error LNK2019: unresolved external symbol "
Like I said, it works in GCC though.
Re: Visual C++ 2008 Express And Templates
Posted: Fri Jul 08, 2011 4:36 pm
by dandymcgee
Code: Select all
template <class T>
class myClass {
public:
myClass() { }
~myClass() { }
void doSomething(T *obj);
};
template <class T>
void myClass<T>::doSomething(T *obj)
{
// Whatever
}
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.
Re: Visual C++ 2008 Express And Templates
Posted: Fri Jul 08, 2011 4:39 pm
by N64vSNES
dandymcgee wrote:Code: Select all
template <class T>
class myClass {
public:
myClass() { }
~myClass() { }
void doSomething(T *obj);
};
template <class T>
void myClass<T>::doSomething(T *obj)
{
// Whatever
}
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
Re: Visual C++ 2008 Express And Templates
Posted: Fri Jul 08, 2011 5:54 pm
by bnpph
This is working fine for me on VS 2008:
Code: Select all
template <class T>
class myClass {
public:
myClass() { }
~myClass() { }
void doSomething(T *obj);
};
template <class T>
void myClass<T>::doSomething(T *obj)
{
// Whatever
}
int main () {
int var;
myClass<int> cl;
cl.doSomething(&var);
return 0;
}
Post the code calling the method.
Re: Visual C++ 2008 Express And Templates
Posted: Sat Jul 09, 2011 6:51 am
by N64vSNES
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.
Thanks.
Re: Visual C++ 2008 Express And Templates
Posted: Sat Jul 09, 2011 9:39 am
by bnpph
You can do it in source files if you know the types you are using. Also if you use a non-standard extension.
Re: Visual C++ 2008 Express And Templates
Posted: Sat Jul 09, 2011 9:52 am
by dandymcgee
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).
A fairly good FAQ regarding templates:
http://www.parashift.com/c++-faq-lite/templates.html
Re: Visual C++ 2008 Express And Templates
Posted: Sat Jul 09, 2011 1:28 pm
by N64vSNES
dandymcgee wrote: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).
A fairly good FAQ regarding templates:
http://www.parashift.com/c++-faq-lite/templates.html
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.
Thanks again.
Re: Visual C++ 2008 Express And Templates
Posted: Sat Jul 09, 2011 4:56 pm
by dandymcgee
N64vSNES wrote:Yeah, I always hate declaring bodies in header files, it seems untidy to me.
I felt the same way when I first had to use them. You'll get over it.