0

I'm trying to implement an operator overload function based on a header file that was given to me, but I'm not understanding one this. Here's what I've been given:

template<class Type>
myClass<Type>& myClass<Type>::operator =(const myClass<Type> &);

My question is in relation to the parameter passed. (const myClass &) indicate a value passed, but how to I reference that value within the function? Normally if I have (const myClass &myValue), I would reference that with myValue=whatever. But I'm not sure how to handle this one.

This is the header file that i'm trying to implement. I'm not asking for anyone to solve this, but I would like some clarifications:  

template<class Type>
struct nodeType{ 
    Type value;
    nodeType<Type> *next;
    nodeType<Type> *prev;
};

template <class Type>
class sortedListADT {
public:
    const sortedListADT<Type>& operator=(const sortedListADT<Type> &);
    
    bool isEmpty() const;
    
    
    bool search(const Type& searchElem) const;
    
    void removeElement(const Type& remElem);
    
        
    void insertElement(const Type& newElem);
    
    Type front() const;
    
    Type back() const;
    
    void printInOrder() const;
    
    void printRevOrder() const;
    
    void destroyList();

    sortedListADT();
    
    sortedListADT(const sortedListADT<Type>& otherList);
    
    ~sortedListADT();

    
private:
    nodeType<Type> *first;
    nodeType<Type> *last;
    int numElements;
    
    void copyList(const sortedListADT<Type>& otherList);

};
  • 1
    Can you provide some more code? Like a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Jason Sep 30 '22 at 16:14
  • 1
    The parameter is `unnamed` so you just can't use it from inside the function. – Jason Sep 30 '22 at 16:15
  • I've edited my post to show the original header file – David Moorse Sep 30 '22 at 16:19
  • I don't see any problem in your code. What exactly are you trying to do? I mean do you want to define the overloaded `operator=` outside the class? – Jason Sep 30 '22 at 16:25
  • Yes I do, this is the header file and my assignment is to implement each function in the .cpp file. Once done, predetermined tests will be run. – David Moorse Sep 30 '22 at 16:27
  • See this [working demo](https://onlinegdb.com/HdpG2IMx3S). I've added the same in my [answer](https://stackoverflow.com/a/73911487/12002570) below. In the answer and the [demo](https://onlinegdb.com/HdpG2IMx3S) you'll find how to implement the overloaded `operator=`. You'll also see that when implementing the overloaded `operator=` you can name the parameter. – Jason Sep 30 '22 at 16:30
  • Actually, in c++ it is possible that a particular parameter does not have any name in declaration of function but have name in the definition/implementation and vice-versa. Usually, many people left out the name of the parameter in the declaration of the function and add the name while defining the function. It is perfectly normal and is just a matter of preference. These things are also explained in a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which are also available as PDFs for free. – Jason Sep 30 '22 at 16:34
  • Ok I wasn't aware that I had the ability to add my own parameter name outside of the header file. I'm still new to this stuff, so I honestly appreciate it. – David Moorse Sep 30 '22 at 16:34
  • Its fine, though I would also recommend using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which are also available as PDFs for free. – Jason Sep 30 '22 at 16:36

1 Answers1

1

how to I reference that value within the function?

We just can't because the parameter is unnamed and for us to use it inside the function it must have a name(to refer to it by that name).

It seems that you're trying to implement the overloaded operator= which you can do as shown below:

template <class Type>
class sortedListADT {
public:
    //this is a declaration
    const sortedListADT<Type>& operator=(const sortedListADT<Type> &);
    
    //other code as before

};
template<typename Type>
//-----------------------------------------------------------------------------------vvvvvvvvvvvvv--->added a name for the paramter
const sortedListADT<Type>& sortedListADT<Type>::operator=(const sortedListADT<Type> &namedParameer)
{
   //add your code here
   //don't forget to have a return statement here 
}

Working demo

Jason
  • 36,170
  • 5
  • 26
  • 60
  • @DavidMoorse Actually, in c++ it is possible that a particular parameter does not have any name in declaration of function but have name in the definition/implementation and vice-versa. Usually, many people left out the name of the parameter in the declaration of the function and add the name while defining the function. It is perfectly normal and is just a matter of preference. – Jason Sep 30 '22 at 16:34