Welcome! Log In Create A New Profile

Advanced

Linker Errors

Posted by BigSteve 
Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Linker Errors
March 16, 2009 07:18PM
Hi Guys

Just Started assignment 2 today , getting a couple of linker errors when i try and compile the project:

[Linker error] undefined reference to `linkedListType<int>::linkedListType()'

The individual files i.e. linkedlist.h , linkedlist.cpp and the main file all compile fine on their own, which leads me to believe there is something wrong specifically with the linker.

I created a new project (console application and selected C++)

I have #include"LinkedList.h" in the .cpp and the main file.

Have any of you come across these types of errors and manged to solve them.

Thanks
avatar Re: Linker Errors
March 17, 2009 08:11AM
BigSteve i've been struggling with this for some time now .. still can't figure this out (getting the same error ) and it frustrating me ..cause i want to see whether my functions required are doing what they're supposed to.I'll try again and report back BUT if someone comes up with a solution ... please don't keep it to yourself !! smile
Re: Linker Errors
March 17, 2009 08:48AM
OK , after struggling with this error for many hours i finally decided to call Unisa and ask someone.
It seems that when you use templates you have to include the code for the header file and the implementation file in one file and not split the two up i.e. do not try and create the LinkedList.cpp and a LinkedList.h in two separate files but rather include the code for both in the LinkedList.h file.

Also is anyone getting the error regarding the friend function for the overloaded << operator ?

[Warning] friend declaration `stdeye popping smileystream& operator<<(stdeye popping smileystream&, const linkedListType<Type>&winking smiley' declares a non-template function

Not sure if its me or if there is something we have to do differently ?

Hops this helps
avatar Re: Linker Errors
March 17, 2009 08:57AM
Yip i also got that error ..if you look at the bottom you can see a whole list of errors of which this is one of them !smile
Re: Linker Errors
March 17, 2009 09:39AM
I combined LinkedList.cpp and LinkedList.h into one file: LinkedList.h, but I still get those damn link errors!
avatar Re: Linker Errors
March 17, 2009 09:45AM
I also tried that ... still getting those errors and with the friend function !! angry smiley
avatar Re: Linker Errors
March 21, 2009 02:53PM
You're working with templates here. You must realise that when it comes to templates, you're not working with real classes, only instructions on how to create classes. The compiler creates the real classes when it compiles them.

For this reason, the entirety of the class - the declaration and the function definitions - must at all times be seen by the compiler when it needs to compile.

The above may or may not make sense. The short version is to put your class and the function definitions - basically everything - inside the header that gets included in whatever file that needs the template.

That will solve your problem.
avatar Re: Linker Errors
March 21, 2009 03:22PM
Sorry BigSteve, looks like you answered that one already.

As far as your warning goes, you need to make your friend function a template too.

Language: C++
template <typename Type> class MyClass {   friend std::ostream& operator<< <Type>(std::ostream &sout, const linkedListType<Type> &list);   public: /* omitted code */ };   template <typename Type> std::ostream& operator<< <Type>(std::ostream &sout, const linkedListType<Type> &list){ /* omitted code */ return sout; }

Basically, if one of a function's parameters is a template class, that function must be a template too, and as such must be defined completely in the header. This is so the compiler knows how to create the real function/class once it knows what the types are.
Re: Linker Errors
March 23, 2009 08:32AM
if I do that I get compile error: 'operator <<' : illegal use of explicit template arguments
avatar Re: Linker Errors
March 23, 2009 05:16PM
Oh Sorry. I just copy-pasted the function declaration to the function itself:

The friend should be:

Language: C++
... operator << <Type> (....)

The function template itself should be:
Language: C++
... operator<< (.....)

I was playing around with it here at work and found that there's some circular referencing going on in this thing so to sort it out, I had to prototype the template class and template operator function so they knew about each other before they were defined. The following code compiles and runs with the Unisa-supplied MinGW compiler:

Language: C++
#include <iostream> using namespace std;   // class and function prototypes template <typename Type> class MyClass; template <typename Type> std::ostream& operator<<(std::ostream &sout, const MyClass<Type> &list);   // class template definition template <typename Type> class MyClass {   friend std::ostream& operator<< <Type>(std::ostream &sout, const MyClass<Type> &list);   public: MyClass(Type value):m_value(value){}   private: Type m_value; };   // function template definition template <typename Type> std::ostream& operator<<(std::ostream &sout, const MyClass<Type> &list){ sout << list.m_value;   return sout; }   // main program entry point int main(int argc, char **argv){ MyClass<int> list(7); // Compiler instantiates "MyClass<int>" in the background using the "MyClass<Type>" template to work from and replacing "Type" with "int"   cout << list << endl; // Compiler instantiates "ostream &operator<< (ostream &, MyClass<int> &)" in the background using the "ostream &operator <<(ostream &, MyClass<Type>)" template   return 0; }
Re: Linker Errors
March 24, 2009 01:25PM
Yes! it works! thanks robanaurochs
avatar Re: Linker Errors
March 24, 2009 01:45PM
I got warnings from the compiler but mine still compiled ... BUT after adjusting it slightly with your suggestion things are working perfectly and really making sense ..
Thumbs up to Rob .. thumbs up

PS! You've been rather quiet these days.. wazzzuuupp ??? eye rolling smiley
avatar Re: Linker Errors
March 24, 2009 01:59PM
Working on a project Wiseguy. Started my own little venture part-time so I usually only have time for the fora at lunch time.
avatar Re: Linker Errors
April 06, 2009 10:31AM
Seems that this problem is an old problem -- > COS211X_2008_phorum_post
smile
Sorry, only registered users may post in this forum.

Click here to login