Johnrad
Johnrad

Reputation: 2655

C++ - Operator Overload Error C4430: missing type specifier - int assumed

I compiled my code using Borland 5.5 and there was no errors that popped up. But it ll didn't run correctly so I decided to use Visual Studio 2010 to debug my program.

Visual studio is giving me this error :

Error   1   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\johnny\documents\visual studio 2010\projects\stack_linkedlist\stack_linkedlist\classstack.cpp  111 1   STACK_LinkedList

It is pointing to my operator overload function. Here is the code to my operator overload.

//operator overload
template <class S>
const Stack<S>::operator=( const Stack& s )
{
    // Check for self assignment
    if (&s==this)
        return *this;

    // Clear the current stack
     while (s.theFront)
        {
            NodePointer p = s.theFront;

            s.theFront = s.theFront->next;
            delete p;
        }

        s.theTop = s.theFront;


    // Copy all data from stack s
    if (!s.isEmpty())
    {
        NodePointer temp = q->theFront;

        while(temp != 0)
        {
            push(temp->data);
            temp = temp->next;
        }
    }

   return *this;
}

Any help would be awesome! Thanks!

Upvotes: 4

Views: 5115

Answers (3)

Vatsan
Vatsan

Reputation: 1173

template <class S>const Stack<S>::operator=( const Stack& s )

In this function declaration, you are missing a return type.

If you are trying to assign Stack objects, try this -

template <class S> 
Stack<S>& Stack<S>::operator=(const Stack& s) 

Overloading the assignment operator has to do two things -

  1. Perform the assignment.
  2. Return *this. This will implicitly support multiple asisgnments of the form a = b = c.

Since you are returning *this, the return type specified in the function declaration has to match the type of *this. In this instance, that would be Stack<S>&.

Upvotes: 1

Xavier V.
Xavier V.

Reputation: 6448

There is no return type defined for your operator.

const Stack<S>::operator=( const Stack& s )

should be changed to :

const Stack<S>& Stack<S>::operator=( const Stack& s )

Upvotes: 9

Jaime Soto
Jaime Soto

Reputation: 3228

Your method is missing the return type. Try this instead:

template <class S>
const Stack<S>& Stack<S>::operator=( const Stack& s )
{
   // body of method
}

Upvotes: 3

Related Questions