LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-15-2007, 10:54 AM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
C++: Can't link template program with separate files for class definition/declaration


I have a really simple problem (I think), but I can't work out for the life of me what it is. I have 3 files:

Code:
// link.h

#ifndef LINK_H
#define LINK_H

template<class T> class Link
{
 private:
  T data;
  Link<T> *nextLink;

 public:
  Link(T d);
  T get();
  void update(T newData);
};

#endif
Code:
// link.cpp

#include "link.h"

template<class T> Link<T>::Link(T d)
{
  data = d;
}

template<class T> T Link<T>::get()
{
  return data;
}

template<class T> void Link<T>::update(T newData)
{
  data = newData;
}
Code:
// linktest.cpp

#include "link.h"

int main()
{
  Link<int> myLink(4);
}
I'm trying to compile this with "g++ linktest.cpp link.cpp -o linktest" and am getting undefined reference errors:

/tmp/ccUiS6RY.o(.text+0x26): In function `main':
/home/nick/C++/linktest.cpp:7: undefined reference to `Link<int>::Link(int)'
collect2: ld returned 1 exit status

If I move the definition of the constructor into link.h, i.e.

Code:
// link.h
...
 public:
  Link(T d) { data = d; }

...
then it compiles and links with no errors.

What am I doing wrong when I'm trying to use separate files?

Thanks!
 
Old 04-15-2007, 12:50 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
see http://www.parashift.com/c++-faq-lit...html#faq-35.12

Quote:
Originally Posted by Nylex
I have a really simple problem (I think)
simple is a word that does not apply to C++
 
Old 04-15-2007, 01:33 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Ye parashift faqs have this nailed, although I would like to comment on something.
Your constructor should be using the initialiser list, otherwise the constructor creates an object of type T and then copies the input value into it.

Code:
  Link(T const& d):data(d){}
It would also be better to return references and constant references, additionally making functions which do not change the class constant functions.
 
Old 04-15-2007, 01:42 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Thanks for the link! I did try with the initialiser list as well, but changed it.
 
  


Reply



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
Indirection in class member declaration websinger Programming 8 09-08-2006 10:24 AM
Java: Placing class members in separate files Orkie Programming 7 01-30-2006 02:11 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
template class ckcheung0927 Programming 3 11-28-2004 03:59 PM
C++ template class syntax rong Programming 12 08-13-2003 12:26 PM

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

All times are GMT -5. The time now is 04:16 AM.

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