代码之家  ›  专栏  ›  技术社区  ›  Johnrad

C++运算符重载错误C44 30:缺少类型说明符-int假设

  •  4
  • Johnrad  · 技术社区  · 15 年前

    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 回复  |  直到 15 年前
        1
  •  10
  •   Xavier V.    15 年前

    没有为运算符定义返回类型。

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

    应改为:

    const Stack<S>& Stack<S>::operator=( const Stack& s )
    
        2
  •  3
  •   Jaime Soto    15 年前

    您的方法缺少返回类型。请改为:

    template <class S>
    const Stack<S>& Stack<S>::operator=( const Stack& s )
    {
       // body of method
    }
    
        3
  •  1
  •   Puppy    15 年前
    template <class S>const Stack<S>::operator=( const Stack& s )
    

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

    堆叠 对象,试试这个-

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

    1. 执行任务。

    由于返回*this,函数声明中指定的返回类型必须与 这个的。在这种情况下 Stack<S>& .