C ++ - 运算符重载错误C4430:缺少类型说明符 - 假定为int

时间:2010-11-01 21:40:34

标签: c++ operator-overloading

我使用Borland 5.5编译了我的代码,并且没有出现错误。但是它没有正确运行所以我决定使用Visual Studio 2010来调试我的程序。

Visual Studio给了我这个错误:

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

它指向我的操作员过载功能。这是我的运算符重载的代码。

//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;
}

任何帮助都会很棒!谢谢!

3 个答案:

答案 0 :(得分:9)

没有为您的运营商定义退货类型。

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

应更改为:

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

答案 1 :(得分:3)

您的方法缺少返回类型。试试这个:

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

答案 2 :(得分:1)

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

在此函数声明中,您缺少返回类型。

如果您尝试分配 Stack 对象,请尝试此操作 -

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

重载赋值运算符必须做两件事 -

  1. 执行作业。
  2. 返回*这个。这将隐式支持a = b = c形式的多个asisgnments。
  3. 由于您返回* this,函数声明中指定的返回类型必须与* this的 type 匹配。在这种情况下,那将是Stack<S>&