Overloading

I am trying to create an overloaded assignment operator so I can assign a linked list from RHS to LHS. I can't run my program through the debugger because I get an error in the clone function on the line that says headPtr = new getPointerTo; .

The error says:
Severity Code Description Project File Line Suppression State
Error C2760 syntax error: unexpected token 'identifier', expected 'type specifier'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
template < class ItemType>
LinkedSet<ItemType>& LinkedSet<ItemType>::operator = (const LinkedSet<ItemType>& rightHandSide)
{ 
    // Check for assignment to self 
    if ( this != &rightHandSide) 
    //Make the assignment operator fail - safe by checking for a special case
        //Overloaded Operators 419
    {
        this->clear(); // Deallocate left-hand side       
        clone(rightHandSide);         // Copy list nodes        
        itemCount = rightHandSide.itemCount; // Copy size of list    
    }   
        return *this; 

 } 


template <class ItemType>
void LinkedSet<ItemType>::clone(const LinkedSet<ItemType>& copyMe) {
    if (copyMe.headPtr == nullptr) {
        headPtr = nullptr;
    }
    else {
        headPtr = new getPointerTo;
        headPtr->data = copyMe.headPtr->data;

        getPointerTo* newListPtr = headPtr;
        getPointerTo* sourcePtr = copyMe.headPtr->next;

        while (sourcePtr != nullptr) {
            newListPtr->next = new node;
            newListPtr = newListPtr->next;
            newListPtr->data = sourcePtr->data;
            sourcePtr = sourcePtr->next;
        }
        newListPtr->next = nullptr;
    }
}
Maybe I should provide the .h file for "node" and "linkedSet." From those you'll be able to see where headPtr and getPointerTo come from.

The operator = function I got from my textbook but it has a generic clone function with no code. The clone function came from my instructor's online notes. I talked to him about patching these together and he says that I am on the right track. However, I think my problem lies in understanding exactly how the clone function works, and the way I am using headPtr, and getPointerTo. I have not changed any code in the algorithm for clone(); only replaced the area where headPtr and getPointerTo are located since the old names are from a different program.

If you would like any other info please let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
template<class ItemType>
class Node
{
private:
    ItemType        item; // A data item
    Node<ItemType>* next; // Pointer to next node

public:
    Node();
    Node(const ItemType& anItem);
    Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
    void setItem(const ItemType& anItem);
    void setNext(Node<ItemType>* nextNodePtr);
    ItemType getItem() const;
    Node<ItemType>* getNext() const;
}; // end Node

#include "Node.cpp"



template<class ItemType>
class LinkedSet : public SetInterface<ItemType>
{
private:
    Node<ItemType>* headPtr; // Pointer to first node
    int itemCount;           // Current count of bag items

    // Returns either a pointer to the node containing a given entry
    // or the null pointer if the entry is not in the bag.
    Node<ItemType>* getPointerTo(const ItemType& target) const;
    void clone(const LinkedSet<ItemType>& copyMe);


public:
    LinkedSet<ItemType>& operator = (const LinkedSet<ItemType>& rightHandSide);
   // void clone(const LinkedSet<ItemType>& copyMe);


    LinkedSet();
    LinkedSet(const LinkedSet<ItemType>& aSet); // Copy constructor
    virtual ~LinkedSet();                       // Destructor should be virtual
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const ItemType& newEntry);
    bool remove(const ItemType& anEntry);
    void clear();
    bool contains(const ItemType& anEntry) const;
    std::vector<ItemType> toVector() const;
}; // end LinkedBag

#include "linkedSet.cpp" 
1
2
3
4
5
int *ptr;
ptr = new int;
//so it follows that
//Node<ItemType> *headPtr;
headPtr = new Node<ItemType>;



now, you have defined getPointerTo as a member function, so I don't understand what were you trying to do
1
2
        getPointerTo* newListPtr = headPtr;
        getPointerTo* sourcePtr = copyMe.headPtr->next;
¿what was your idea with those lines?
I was getting lost at that point, but I now see that getPointerTo* should be Node<ItemType>*. Along with some further modifications the function is completed now. Thank you.
Topic archived. No new replies allowed.