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!
std::queue. Is there some reason you are trying to re-invent this wheel (...poorly) that is already provided by the language?if (first == right.first)... what isfirst? You don't have a member variable with that name.