简体   繁体   English

错误:成员 function 不得在其 class 之外声明。

[英]Error: member function may not be declared outside of its class.

I have my heap.h file that contains:我的 heap.h 文件包含:

bool insert(int key, double data);

and in my heapCPP.cpp file I have:在我的 heapCPP.cpp 文件中,我有:

 bool heap::insert(int key, double data){
    bool returnTemp;
    node *temp = new node(key, data);

    returnTemp = insert(temp);
    delete temp;
    return returnTemp;
}

However, I get a error saying "member function "heap::insert" may not be redeclared outside its class.但是,我收到一条错误消息,提示“成员 function “heap::insert”可能无法在其 class 之外重新声明。

您可能忘记了 cpp 中的右括号,这可以解释为在另一个函数中重新声明它。

The error message is clear enough.错误信息已经足够清楚了。 If function insert is a member function of class heap it shall be at first declared in the class definition.如果函数 insert 是类堆的成员函数,则应首先在类定义中声明。

For example例如

class heap
{
    //...
    bool insert(int key, double data);
    //,,,
};

Take into account that you are using one more function with name insert inside the body of the first function考虑到您在第一个函数的主体内使用了另一个名称为 insert 的函数

returnTemp = insert(temp);

So it seems you have some mess with function declarations and definitions.因此,您似乎对函数声明和定义有些混乱。

I had the same issue.我遇到过同样的问题。 Check your function definitions before and after your "insert" function definition.在“插入”函数定义之前和之后检查函数定义。 Make sure to include closing brackets for your previous function definitions.确保为之前的函数定义包含右括号。

My function definition was nested inside another definition which caused my error.我的函数定义嵌套在另一个定义中,这导致了我的错误。

I had the same error message, but in my case the problem was caused by a semi-colon in the .cpp file (from a bad copy and paste).我有相同的错误消息,但在我的情况下,问题是由 .cpp 文件中的分号引起的(来自错误的复制和粘贴)。 That is, there was a semi-colon at the end of the function signature in the cpp file.也就是说,cpp 文件中函数签名的末尾有一个分号。

If I use your code as an example then,如果我以您的代码为例,

heap.h:堆.h:

bool insert(int key, double data);

heapCPP.cpp: heapCPP.cpp:

bool heap::insert(int key, double data);
{
    // ...
}

Fix it with this in heapCPP.cpp:在 heapCPP.cpp 中修复它:

bool heap::insert(int key, double data)
{
    // ...
}

For me, this same error appeared after I removed the topmost initializer from a constructors initializer list after it had become obsolete.对我来说,在我从构造函数初始化器列表中删除最顶层的初始化器后,它已经过时,出现了同样的错误。 My code formatting for constructor definitions looks like this for better readability:为了更好的可读性,我的构造函数定义的代码格式如下所示:

c_MyClass::c_MyClass()
  : m_bSomeMember{ true }
  , m_bAnotherMember{ false }
{}

The class I worked with had a lot more initializers, so when I removed the top one as it was no longer needed, I didn't realize that I accidentally removed the colon at the start of the line as well and was left with:我使用的 class 有更多的初始化程序,所以当我删除了不再需要的顶部的初始化程序时,我没有意识到我不小心删除了行首的冒号并留下了:

c_MyClass::c_MyClass()
  , m_bAnotherMember{ false }
{}

Which isn't a constructor definition at all and caused the error.这根本不是构造函数定义并导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM