Tim*_*all -2 c++ constructor compiler-errors
我有一个有用的函数,我想将它从独立实用程序转换为 RAII 样式的帮助程序类。我对类的定义进行得很顺利,直到我粘贴函数代码并重命名它。此时函数名用红色下划线标出,工具提示说“与其类同名的成员函数必须是构造函数”。
此错误消息没有帮助。我知道我不能编写与类同名的函数。我希望这个函数是一个构造函数。为什么不是?这是怎么回事?
前:
void Useful( int Param ) // works, and is useful
{
// do useful things
}
Run Code Online (Sandbox Code Playgroud)
后:
class Useful
{
void Useful( int Param ) // generates error
{
// do useful things
}
};
Run Code Online (Sandbox Code Playgroud)
问题是剪切和粘贴错误。返回类型的存在阻止了函数被解释为构造函数。
所以:
class Useful
{
Useful( int Param ) // problem solved
{
// do useful things
}
};
Run Code Online (Sandbox Code Playgroud)