LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-31-2015, 01:23 AM   #1
rajkar
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Rep: Reputation: Disabled
Post Undefined referenceto a Template function in Linux.


hi,
I m using c++ code in Linux OS with gcc compiler.

The class is in Template, the declaration & definition is in header file.
The calling function is in another cpp file.

Ex:template <class T> void samplefunc(T *t, int nbnumb,int ndlay,state *cs,result *rs);//declared in header

template <class T> void samplefunc(T *t, int nbnumb,int ndlay,state *cs,result *rs)//defined in header
{

}
samplefunc(temp->Getp(),temp->nbts,type:oint,cs,rs);//calling func in cpp file.

ERROR: while calling it , it displays the error like: undefined reference to
'void class name::samplefunc<type>(t*,int,int,cs*,rs*)'

How to solve this issue?...
 
Old 03-31-2015, 02:39 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
as to your issue ,not sure
BUT
the version of gcc you are using and the Operating system and version would help others

if for example you are using a VERY old version of gcc ( say gcc 3.3 or 4.0 ) the fix might be different than for the NEW gcc4.9

Code:
gcc -V
--- and this ---
uname -a
 
Old 03-31-2015, 03:38 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I strongly doubt the gcc version or linux version are relevant.

The exact error message and exact code (rather than approximations of each) might help a lot.

Quote:
Originally Posted by rajkar View Post
The class is in Template, the declaration & definition is in header file.
The calling function is in another cpp file.
What hides behind that odd wording? Is there a significant cpp file you haven't mentioned?

Your approximation of the error message makes me guess the compiler had seen the declaration and not the definition, but what you said about hpp contents makes that seem incorrect.
Without exact info, I'm just guessing.

Edit: I just figured out what you are saying and understand the problem and of course the compiler has seen the declaration and not the definition, just as the error message shows. Because you defined a different function than you declared.

The function declaration is inside a class definition, but the function definition is outside the class definition, so it needs to specify className:: to qualify the function name.

Exact (copy paste) of code and messages inside [CODE] blocks in your post would have made all that instantly obvious rather than take guesses at the relationship between what you had done and what you had posted.

Last edited by johnsfine; 03-31-2015 at 03:50 PM.
 
Old 04-01-2015, 08:32 AM   #4
rajkar
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
hi,
Thanks for ur help... No issues in gcc version.
I m using Template class function in header file which includes a declaration and Implementation.
And calling that function in cpp file(calling function).

ERROR: undefined reference to a function.


Is it clear.. How to solve this?
 
Old 04-01-2015, 02:01 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by rajkar View Post
Is it clear.. How to solve this?
If I correctly estimated what you did from what you posted, then I already told you how to solve it: Add the className:: infront of the function name where you define the function.

If I did not correctly estimate what you did based on what you posted, or if you don't like my advice and hope to get help from someone else, or for any reason want a better answer, you need to post clearer details of what you did and what error message you got.

Quote:
it displays the error like: undefined reference to
'void class name::samplefunc<type>(t*,int,int,cs*,rs*)'
I'd be a lot more sure if you posted exact copy paste of the error message. But in what you posted I see :: where it indicates your function was declared inside a class definition.

Quote:
template <class T> void samplefunc(T *t, int nbnumb,int ndlay,state *cs,result *rs)//defined in header
Again an exact copy/paste of your code inside [CODE] tags would make it a lot easier to be sure we understand what you did. But what you showed was a definition for a function that is not a member of a class, so you did not define the function you declared
 
Old 04-01-2015, 02:10 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Sample code (untested, I don't have g++ on the computer where I am now):

Code:
class className {
public:
  template <class argType> void fun(argType*);
};
template <class argType> void className::fun(argType*)
{}
int main()
{
  className dummy;
  dummy.fun("something");
  return 0;
}
If you left out the red part in my sample code, I think that creates the error you described, so inserting the equivalent of that red part (with your class name) should fix that.

Last edited by johnsfine; 04-01-2015 at 02:15 PM.
 
  


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
C++: different instances of a function template unihiekka Programming 3 02-05-2009 01:50 PM
PHP Problem ('undefined index', 'undefined function') zokken Programming 2 12-04-2008 10:18 AM
C++ Template For Generic Function - Is This Possible? taylor_venable Programming 6 06-05-2006 08:46 PM
"undefined reference" to a template class function btb Programming 3 08-25-2005 05:02 PM

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

All times are GMT -5. The time now is 01:44 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