LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2006, 08:34 PM   #1
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Rep: Reputation: 15
C++ template blues


It's been a while since I've programmed for school, and after picking up a copy of Sedgewick I decided to try out one of the problems for kicks. The class I wrote works great for concrete types, but when I tried to make a templated version I couldn't get it to link, as it couldn't resolve function calls to the templated class. Here's a stripped down version of the example.

Here's the makefile:

Code:
exe=polyadd
obj=polyadd.o polynomial.o

$(exe): $(obj)
	c++ $(LDFLAGS) -o $(exe) $(obj)
	
polynomial.o: polyadd.c++ polynomial.c++
	c++ $(CXXFLAGS) -c polynomial.c++

polyadd.o: polyadd.c++ polynomial.h
	c++ $(CXXFLAGS) -c polyadd.c++

clean:
	-rm -f $(obj) $(exe)
Here's the file err.fuck after running the command make 2>err.fuck
Code:
polyadd.o: In function `main':
polyadd.c++:(.text+0x124): undefined reference to `polynomial<int>::polynomial(std::vector<int, std::allocator<int> >&)'
polyadd.c++:(.text+0x137): undefined reference to `polynomial<int>::print(std::basic_ostream<char, std::char_traits<char> >&) const'
collect2: ld returned 1 exit status
make: *** [polyadd] Error 1
And here's the code:

Code:
/** polynomial.h */
#ifndef	POLYNOMIAL_H
#define	POLYNOMIAL_H


#include	<vector>
	using	std::vector;
#include	<ostream>
	using	std::ostream;

	
template <class T>
class
polynomial
{
	public:
		T&	get_component (int n) const;
		int	degree () const;
		polynomial (vector<T> &);
		void	print (ostream &) const;
	private:
		vector<T>	p;
};

#endif
Code:
/** polynomial.c++ */
polynomial<T>::polynomial (vector<T> &v)
{
	p = v;
}


template <class T>
T&
polynomial<T>::get_component (int n) const
{
	return	p [n];
}


template <class T>
int
polynomial<T>::degree () const
{
	return	p.size ()-1;
}


template <class T>
void
polynomial<T>::print (ostream &os) const
{
	if (p.size ()==0)
		return;
	else if (p.size ()==1)
		os << p[0];
	else {
		os << p[0] << " + ";

		for (int i=1; i < degree (); i++)
			os << p[i] << "x^" << i << " + ";
		os << p [degree ()] << "x^" << degree ();
	}
}

Code:
/** polyadd.c++ */

#include	<iostream>
	using	std::cout;
	using	std::endl;
	using	std::cerr;
#include	"polynomial.h"
#include	<vector>
	using	std::vector;



int
main (int argc, char *argv [])
{
	vector<int>	v;
	v.push_back (1);
	v.push_back (2);
	v.push_back (3);
	v.push_back (4);	// 1 + 2x + 3x^2 + 4x^3

	for (int i=0; i<v.size (); i++)
		cout << v[i] << " ";
	cout << endl;
	
	polynomial<int>	p(v);

	p.print (cout);

	return	0;
}
Please tell me I'm just doing something stupid. I remember doing stuff like this in Visual C++ for my class. I've tried it under gcc-4.0.3 and gcc-3.3.6, and the output I posted is from compiling with gcc-4.0.3.

Thanks
 
Old 06-07-2006, 08:55 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
See this thread. In short you need to have the definition and declaration of your template functions in the same file
 
Old 06-07-2006, 09:20 PM   #3
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Original Poster
Rep: Reputation: 15
so it's a g++ bug
 
Old 06-07-2006, 09:34 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by spursrule
so it's a g++ bug
No, not quite. Stroustrup mentions this problem in "The C++ Programming Language" (I did some more research after I solved my problem): he says that if you want to separate your template declarations and definitions, use the "export" keyword in front of the definition. However, that doesn't really change how you have to write the code, because GCC does not support the "export" keyword yet. Apparently, it's very hard to get to work correctly, probably for the reasons that graemef mentions in that other thread. So GCC actually handles the template correctly, but unfortunately does not correctly handle the mechanism used to make up for the shortcoming in the language. So yeah, you have to put them both in the same file, but it's not (at least not immediately) GCC's fault.
 
Old 06-07-2006, 09:48 PM   #5
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Original Poster
Rep: Reputation: 15
Wow... that's hard to believe, since my copy of Strousstrup is 9 years old and mentions exactly that in chapter 13. Does Intel's C++ compiler handle templates correctly?
 
Old 06-08-2006, 08:39 AM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
According to the Wikipedia article on C++ only two compilers have ever supported the export keyword, Comeau C++ (never heard of that one myself) and a version of Borland C++ Builder. This InformIT article (although marked as "outdated") notes that in practice the export feature actually doesn't do what it sounds like, because the real meat of the template is in the implementation, not the prototype. Taken all together, it sounds like this feature was simply too much for existing compiler technology to cope with.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Daemon Template thekonqueror Programming 0 08-15-2005 02:07 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
user template bogus__13 Linux - General 5 01-21-2005 01:25 AM
template class ckcheung0927 Programming 3 11-28-2004 03:59 PM
c++ template help ashirazi Programming 4 11-16-2004 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration