错误:成员函数不能在其类之外声明
Error: member function may not be declared outside of its class.
我有我的 heap.h 文件,其中包含:
bool insert(int key, double data);
在我的堆CPP.cpp文件中,我有:
bool heap::insert(int key, double data){
bool returnTemp;
node *temp = new node(key, data);
returnTemp = insert(temp);
delete temp;
return returnTemp;
}
但是,我收到一个错误,说"成员函数"heap::insert"可能无法在其类之外重新声明。
您可能忘记了 cpp 中的右括号,这可以解释为在另一个函数中重新声明它。
我有同样的错误消息,但就我而言,问题是由.cpp文件中的分号引起的(来自错误的复制和粘贴)。也就是说,cpp 文件中函数签名的末尾有一个分号。
如果我使用您的代码作为示例,那么,
堆:
bool insert(int key, double data);
heapCPP.cpp:
bool heap::insert(int key, double data);
{
// ...
}
在堆CPP中修复它.cpp:
bool heap::insert(int key, double data)
{
// ...
}
错误消息足够清晰。如果函数插入是类堆的成员函数,则应首先在类定义中声明。
例如
class heap
{
//...
bool insert(int key, double data);
//,,,
};
考虑到您正在使用另一个函数,并在第一个函数的主体中插入名称
returnTemp = insert(temp);
所以你似乎对函数声明和定义有些混乱。
我遇到了同样的问题。检查"插入"函数定义前后的函数定义。请确保为以前的函数定义包含右括号。
我的函数定义嵌套在另一个定义中,这导致了我的错误。
对我来说,同样的错误出现在我从构造函数初始值设定项列表中删除后,它已经过时了。为了提高可读性,构造函数定义的代码格式如下所示:
c_MyClass::c_MyClass()
: m_bSomeMember{ true }
, m_bAnotherMember{ false }
{}
我使用的类有更多的初始值设定项,所以当我删除顶部的初始化器时,因为它不再需要,我没有意识到我不小心删除了行首的冒号,只剩下:
c_MyClass::c_MyClass()
, m_bAnotherMember{ false }
{}
这根本不是构造函数定义并导致错误。
我遇到了同样的错误,然后我发现在我的类中函数声明之后定义成员函数时忘记了半列:
class Fixed{
private:
int number;
static const int fraction = 8;
public:
Fixed(const int integer);
Fixed(const Fixed& obj);
Fixed(const float number);
Fixed();
~Fixed();
Fixed& operator=(const Fixed& pos);
int toInt(void) const;
float toFloat(void) const;
int getRawBits(void) const;
void setRawBits(int const raw);
//extra overloading
bool operator>(const Fixed& pos) const;
bool operator<(const Fixed& pos) const;
bool operator>=(const Fixed& pos) const;
bool operator<=(const Fixed& pos) const;
bool operator==(const Fixed& pos) const;
bool operator!=(const Fixed& pos) const;
Fixed operator+(const Fixed& pos) const;
Fixed operator-(const Fixed& pos) const;
Fixed operator*(const Fixed& pos) const;
Fixed operator/(const Fixed& pos) const;
Fixed& operator++(void);
Fixed operator++(int);
Fixed operator--(int);
Fixed& operator--(void);
};
我犯的错误:
Fixed& Fixed::operator--(void);
{
this->number--;
return (*this);
}
可以通过删除半列来修复它:
Fixed& Fixed::operator--(void)
{
this->number--;
return (*this);
}
相关文章:
- 在指向现有内存地址的 hpp 文件中声明成员函数的最佳方法
- 如何使用模板声明成员函数?(不是模板类)
- 如何使用 Boost.Hana 声明成员变量
- 声明成员对象而不调用其默认构造函数
- 使用函数类型语法声明成员函数
- 尝试访问标头声明成员时出现隔离错误
- 根据二手构造函数声明成员
- C 模板明确声明成员函数值/避免了宏的问题
- 如何在基本模板类中声明成员,其中类型取决于派生类的类型
- 错误:类中没有声明成员函数
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 从variadic模板参数声明成员变量
- 在类(C )中使用模板时未声明成员函数
- 错误:类中未声明 '' 成员函数
- 如何在指向常量地址时声明成员函数常量指针
- 何时以及为什么在堆C++上声明成员变量
- C++ 错误:未在范围内声明;成员函数
- 在不声明成员变量的情况下更改按钮颜色
- 错误 C2535:已定义或声明成员函数
- 当声明成员函数时,*New()是什么意思