Fedora Linux Support Community & Resources Center
  #1  
Old 17th March 2009, 03:10 PM
xtcsin Offline
Registered User
 
Join Date: Aug 2008
Posts: 16
undefined reference when link with g++

I write two files:
Quote:
//SelectSort.cpp
#include<iostream>

using namespace std;

template <class T>
void SelectionSort(T p[],int n)
{
}
Quote:
//main.cpp
#include<iostream>
#include<iomanip>

using namespace std;
template <class T>
void SelectionSort(T p[],int n);

int main()
{

int p[8]={1,7,3,6,2,8,3,4};
SelectionSort<int>(p,8);

return 0;
}
then i compile the sorce file with
Quote:
g++ -c SelectSort.cpp -o SelectSort.o
g++ -c main.cpp -o main.o
g++ -o main main.o SelectSort.o
the error when linking is :
Quote:
main.o: In function `main':
main.cpp.text+0x9c): undefined reference to `void SelectionSort<int>(int*, int)'
collect2: ld returned 1 exit status
Reply With Quote
  #2  
Old 17th March 2009, 03:37 PM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,076
You must include the template class definition in the file it's used instead of trying to compile it separately. See: http://www.parashift.com/c++-faq-lit...html#faq-35.12
Reply With Quote
Reply

Tags
link, reference, undefined

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc: undefined reference to 'crypt' giftiger_wunsch Using Fedora 22 3rd June 2009 12:08 PM
undefined reference to whilst using GCC claudius1974 Using Fedora 2 23rd August 2008 01:55 PM
undefined reference to `pthread_atfork' Sidrian Programming & Packaging 7 19th September 2007 12:31 PM
undefined reference to `gpg_strerror' yinglcs Using Fedora 0 18th June 2007 06:11 AM
gcc - undefined reference to `__gxx_personality_v0' Monika Using Fedora 11 16th January 2006 08:50 AM


Current GMT-time: 19:47 (Thursday, 23-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat