943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2290
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 11th, 2008
0

Undefined reference error

Expand Post »
I am trying to implement a queue based on a Linked List that I had to write previously. It is templated and for some reason I am getting the following error using g++ when I compile:

C++ Syntax (Toggle Plain Text)
  1. g++ LinkedQueueMain.cpp -o LinkedQueueMain
  2. /tmp/ccqwStpK.o: In function `main':
  3. LinkedQueueMain.cpp:(.text+0x2ee): undefined reference to `LinkedQueue<char>::operator=(LinkedQueue<char> const&)'
  4. collect2: ld returned 1 exit status

It is saying undefined reference however I have declared and defined it so I cant figure out what the problem is.

This is my class declaration and definition:
// LinkedQueue.h

#ifndef LINKEDQUEUE_H
#define LINKEDQUEUE_H

#include <iostream>
#include "RuntimeException.h"
#include "LinkedList.h"

template<typename T> class LinkedQueue;

template<typename T>
std::ostream& operator<<(std::ostream& out, const LinkedQueue<T>& queue);

template<typename T>
class LinkedQueue {
private:
   LinkedList<T> ll;

public:
   // user-defined exceptions
   class QueueEmptyException : public RuntimeException {
   public:
     QueueEmptyException() : RuntimeException("Access to an empty queue") {}
   };

   LinkedQueue() { } // constructor
   ~LinkedQueue() { } // destructor
   LinkedQueue(const LinkedQueue& queue) { ll = queue.ll; } // copy constructor
   LinkedQueue& operator=(const LinkedQueue& queue); // assignment operator
   
   // I have cut out accessory function declarations
   
   friend std::ostream& operator<< <>(std::ostream& out, const LinkedQueue<T>& queue); // overload <<
};

//-------------------------------------------------------------------------------------------------------------------------

template<typename T>
LinkedQueue<T>& LinkedQueue<T>::operator=(const LinkedQueue& queue) {
	ll = queue.ll;
	return *this;
}

// other functions defined here I just cut them out for space

#endif

Here is my main:

#include "LinkedQueue.h"
#include <iostream>
#include <string>
#include <iterator>
using namespace std;

int main()
{
  LinkedQueue<char> queue;
  LinkedQueue<char> queue_copy;

  //===== enqueue() =====
   
  queue.enqueue('1');
  queue.enqueue('2');
  queue.enqueue('3');
  queue.enqueue('4');
  queue.enqueue('5');
  queue.enqueue('6');
   
  queue_copy = queue;  //THIS IS WHERE I BELIEVE THE PROBLEM IS
  cerr << "assigning queue to queue_copy, queue_copy = ";
  cerr << queue_copy << endl;
  cerr << "size of queue_copy = " << queue_copy.size() << endl;
  cerr << "first of queue_copy = " << queue_copy.first() << endl << endl;

return 0;
}

Thanks in advance for any help.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 11th, 2008
0

Re: Undefined reference error

I am not sure about this , But Why returning (*this)? In your = operator.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 11th, 2008
1

Re: Undefined reference error

Are you sure this code works:
C++ Syntax (Toggle Plain Text)
  1. ll = queue.ll;
'll' is under private section, right? I'm not sure if you can access it.
Here's my code example if it helps:
C++ Syntax (Toggle Plain Text)
  1. //operators
  2. //=
  3. Complex& Complex::operator=(Complex const& aCplx){
  4. if (this != &aCplx){
  5. mNum.Im = aCplx.getIm();
  6. mNum.Re = aCplx.getRe();
  7. }
  8. return *this;
  9. }
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 11th, 2008
0

Re: Undefined reference error

I am not sure about this , But Why returning (*this)? In your = operator.
(*this) is returned so you can write things like:
myObjA = myObjB = myObjC;
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 11th, 2008
0

Re: Undefined reference error

Our professor gave us a skeleton of the code and it already had the return statement in it so I didn't change it. The LinkedList = operator overload returns a pointer as well.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 11th, 2008
0

Re: Undefined reference error

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
Are you sure this code works:
C++ Syntax (Toggle Plain Text)
  1. ll = queue.ll;
'll' is under private section, right? I'm not sure if you can access it.
Here's my code example if it helps:
C++ Syntax (Toggle Plain Text)
  1. //operators
  2. //=
  3. Complex& Complex::operator=(Complex const& aCplx){
  4. if (this != &aCplx){
  5. mNum.Im = aCplx.getIm();
  6. mNum.Re = aCplx.getRe();
  7. }
  8. return *this;
  9. }
Well the definition is within the class and classes can access their own private data members so ya it should work. I tried putting it in the public section just to check and it did the same thing. As to your example thats basically the same thing that im trying to do. I don't see why mine doesnt work.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 11th, 2008
0

Re: Undefined reference error

Click to Expand / Collapse  Quote originally posted by chunalt787 ...
Well the definition is within the class and classes can access their own private data members so ya it should work. I tried putting it in the public section just to check and it did the same thing. As to your example thats basically the same thing that im trying to do. I don't see why mine doesnt work.
I'm not sure about this.
Function can access IT'S own members (this->memb1; this->memb2
But I'm really not sure about accessing other instance of same type...

Maybe your problem is in something else...
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 11th, 2008
1

Re: Undefined reference error

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
I'm not sure about this.
Function can access IT'S own members (this->memb1; this->memb2
But I'm really not sure about accessing other instance of same type...

Maybe your problem is in something else...
I have done something very similar to that previously so I am pretty sure thats not the problem.

BTW Thanks to everyone thats been trying to help. This is getting really frustrating.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 11th, 2008
0

Re: Undefined reference error

Zip and post entire code, please.
It's frustrating me now too
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 13th, 2008
0

Re: Undefined reference error

I think its zipped now thats the first time I have done that. I am sorry its been so long. I had an emergency that kept me from the computer for a couple days. Im back now and still annoyed by this error.
Attached Files
File Type: zip LinkedQueue.zip (1.4 KB, 28 views)
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ with GSL help
Next Thread in C++ Forum Timeline: VS 2005 - Working with templates and lists error.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC