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

发布于 2024-09-29 19:30:49 字数 1095 浏览 3 评论 0原文

我使用 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;
}

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-10-06 19:30:49

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

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

应改为:

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

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 )
空城仅有旧梦在 2024-10-06 19:30:49

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

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

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
}
欢烬 2024-10-06 19:30:49
模板 const Stack::operator=( const Stack& s )


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

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

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

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

  1. 执行赋值。
  2. 返回*这个。这将隐式支持 a = b = c 形式的多重赋值。

由于您要返回 *this,因此函数声明中指定的返回类型必须与 *this 的 type 匹配。在本例中,这将是 Stack&

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>&.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文