1

I'm trying to figure out why this operator is giving me trouble. I actually allowed Visual Studio to build the function definition for me, so I'm not sure why it's upset at me. There's also an issue with recognizing my private Node pointer "first" so if you know why it's upset with that, please let me know!

this is the definition: Queue& operator=(const Queue& right); it's public within the Queue template.

template <class ElementType>
class Queue
{
public:
Queue();
virtual ~Queue();
Queue(const Queue& orig);

Queue& operator=(const Queue& right);

bool enqueue(const ElementType& a);
ElementType peekFront() const;
bool dequeue();
bool isEmpty() const;
int Length() const;

void Display(ostream& out) const;

private:

class Node
{
public:
    ElementType data;
    Node* next;
};

Node* front;
Node* back;
};


//here's the overloaded operator. Visual Studio is upset with its own created definition...

template<class ElementType>
inline Queue& Queue<ElementType>::operator=(const Queue & right)
{
//https://www.geeksforgeeks.org/cpp-assignment-operator-overloading/# referenced for operator   
if (first == right.first) //either they point to the same place or both are nullptrs
    //either way, nothing left to do
    return;
else if (first != nullptr)
{
    while (first != nullptr)
        dequeue();
} //now left is an empty queue and copies can be made!

Node* p = right.first;

while (p != nullptr)
{
    enqueue(p->data); //will insert data into left queue
    p = p->next; //moves through right queue
}
front = p; //now front gets p! back should've updated with each enqueue
return;
}

I tried several different definitions before allowing Visual Studio to make its own. I tried putting the function within the class, and attempted to research why it wasn't working. I turned up nothing, however. For the 'first' node ptr problem, I'm confused because it throws errors but does not add a red underline to any of the uses of first. I'm less concerned about that right now, only because I think the operator is a bigger issue. Resolving it might fix the pointer issue, but idk. for first, it seems to not like being asked: if (first == nullptr), if that helps.

Edited to add: I'm working on a homework assignment--sorry I didn't add that initially. I'm currently learning C++, so I apologize for anything that seems weird/jarring.

Edit 2: I figured out the "first" pointer problem. It's supposed to be called front! ugh, what a silly mistake--that's now resolved. Thanks everyone for your help with all this. I didn't want to bother my professor over the weekend, and you have all been so helpful! I'm currently reworking my queue template (removing the elementtype for now) and I'll update this when I've gotten the base part to work!

Edit 3: Okay, so I've straightened out all of my other code and I'm still having a problem with this dang operator. Here's the new code for the function (declaration within class is the same):

template<class ElementType>
inline Queue& Queue<ElementType>::operator=(const Queue & right)
{
    //asked a question on StackOverflow: 
https://stackoverflow.com/questions/79496248/c-overloading-operator-for-a-linked-list-queue
//https://www.geeksforgeeks.org/cpp-assignment-operator-overloading/# referenced for operator   
    return Queue(right);
}

The errors that have arisen are: "C2955: Queue use of class template list requires template argument list." and "C2244: 'Queue::operator=': unable to match function definition to an existing declaration."

I have no idea what these compiler errors need from me... does anyone know what is causing these errors? It doesn't matter what I put within the function--the error is in the "inline Queue& Queue::operator=(const Queue & right)" part...

thanks again for everyone's help; I've learned a lot today from all of your comments, resources, and advice!

20
  • 2
    Your problem mostly is a conceptual one: If you're going to make a class with raw pointers to its data and you're adding copy constructor. You're going to end up with problems. In C++ never use "owing raw pointers", use std::unique_ptr (or std::shared_ptr). Also learn about the rule of 3/5/0 Side note : Why don't you just use a std::vector (std::list) for your queue. Or even go for using fully tested code and use std::queue Commented Mar 9 at 16:54
  • 1
    Who or what is teaching you C++? Commented Mar 9 at 16:55
  • 2
    C++ already comes with a std::queue. Is there some reason you are trying to re-invent this wheel (...poorly) that is already provided by the language? Commented Mar 9 at 16:59
  • 3
    if (first == right.first) ... what is first? You don't have a member variable with that name. Commented Mar 9 at 17:44
  • 2
    You return nothing from function that returns Queue&. And what created this code, it looks like an AI hallucination when it wasn't told correctly what to do. Commented Mar 9 at 17:45

2 Answers 2

2

You're on the right track buddy, but there are a few fixes needed:

  1. Use if (this == &right) return *this; to avoid unnecessary work.

  2. Your cleanup should use while (!isEmpty()) dequeue();.

  3. Instead of manually setting front = p, use enqueue(p->data) in a loop.

    template<class ElementType>
    Queue<ElementType>& Queue<ElementType>::operator=(const Queue& right) {
        if (this == &right) return *this;
    
        while (!isEmpty()) {
            dequeue();
        }
        for (Node* p = right.front; p; p = p->next) {
            enqueue(p->data);
        }
    
        return *this;
    }
    
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I like the enqueue(p->data) suggestion a lot, thank you!! I don't have to make an == operator (per assignment instructions) though so I'm trying to wriggle around having to write one
@Maddie You don't need to add an operator== overload for this == &right. It's built-in.
@TedLyngmo okay thank you so much!!
@Maddie It is not just that you don't need to write one, it is that you shouldn't write one. Both this and &right are pointers. If they are equal, it means that the right hand side is exactly the same object as the one you are assigning to. Pointer equality comparison is well-defined and you shouldn't mess with it. Operator overloading are for objects of user-defined types, not for pointers to them. If you need to tell a compiler a custom way to compare two objects of your type (two Queues, not two Queue*s), that's when you need to overload operator==.
@WeijunZhou Thank you--I forgot I was working with pointers and not the Queue class/object. I had a migraine yesterday and thinking was hard haha. I did not write one thankfully!
0

This line of code:

front = p; //now front gets p! back should've updated with each enqueue

Does not quite make sense to me. Doesn't your enqueue() take care of all these details already?

1 Comment

yeah, it does; I think this is a redundancy. The body of the operator = was more pseudocode than anything so I could remember what needed to happen. I've moved some things around to make the code more efficient--by just calling enqueue for example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.