Visual C++ 2008 Express And Templates

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Visual C++ 2008 Express And Templates

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Visual C++ 2008 Express And Templates

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Visual C++ 2008 Express And Templates

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Visual C++ 2008 Express And Templates

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Visual C++ 2008 Express And Templates

Post 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 :|
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: Visual C++ 2008 Express And Templates

Post 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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Visual C++ 2008 Express And Templates

Post 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.
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: Visual C++ 2008 Express And Templates

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Visual C++ 2008 Express And Templates

Post 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
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Visual C++ 2008 Express And Templates

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Visual C++ 2008 Express And Templates

Post 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. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply