Error: expected constructor, destructor, or type conversion before '' token?
Error: expected constructor, destructor, or type conversion before '' token?
我一直收到这个错误,我不确定如何解决它。我是初学者,正在尝试构建代码来练习运算符重载。首先,我有错误说存在对 FeetInches(unsigned int= 0, int= 0) 的未识别引用。然后我尝试实现构造函数并得到了那个错误
#include<iostream>
#include<cstdlib>
using namespace std;
class FeetInches
{
private:
unsigned int feet;
int inches;
void simplify();
public:
FeetInches(unsigned int= 0, int= 0);
FeetInches operator+(const FeetInches &);
void display();
};
void FeetInches::simplify()
{
if (inches >=12)
{
feet+= (inches/12);
inches = inches % 12;
}// end of IF
else if (inches <0)
{
feet-=((abs(inches)/12)+1);
inches = 12-(abs(inches)%12);
}// end of elseif
}// end of simplify
FeetInches FeetInches::operator+(const FeetInches & right)
{
FeetInches one (10,15);
FeetInches ruler(5,3);
FeetInches temp;
temp=one+ruler;// temp one.operator +(ruler)
temp.display();
}
FeetInches::FeetInches(feet =0,inches=0)
{
}
void FeetInches::display()
{
cout << "F: " << feet << " I:" << inches << endl;
}
int main()
{
FeetInches aMeasure;
aMeasure.display();
return 0;
};
语法应该是
FeetInches(unsigned int feet = 0, int inches = 0);
用于声明,而不是
FeetInches::FeetInches(unsigned int feet, int inches)
: feet(feet), inches(inches)
{ }
在定义中。
原因是技术上的,而不是逻辑上的,它们取决于 C++ 在较低级别的实现方式。
参数的默认值例如由函数的调用者提供,因此必须在函数的声明中指定函数,因为在 C++ 中,不能保证编译器在编译对函数的调用时知道函数的实现。
很明显,您没有将构造函数 body 附加到您的代码中,为此您可以这样写:
class type_name{
public :
type_name (...ARGS...){ ... }
};
或这个
class type_name{
public :
type_name (...ARGS...);
};
type_name(... ARGS ...){ ...body.. }
所以您需要附加 body 否则链接器不会生成二进制文件,这就是它背后发生的事情:
1- 然后调用编译器为您编写的每个 { .c/.cpp/.cxx ... } 生成一个 .obj 文件。
2- 然后调用链接器来创建最终输出,为此他需要在任何 .obj 文件中为每个调用的函数找到引用。如果您只编写函数声明而不将其称为链接器可能只会发出警告,但如果您调用它,他需要找到它的代码。因为他需要用代码地址替换调用。如果没有代码,他会给你一个未定义的引用错误。
对于每个函数代码,都有一个 header 说明要传递给该代码的参数和类型。他告诉你要找到函数 body 。你在声明 "FeetInches(unsigned int= 0, int= 0);" 中写了 header ,然后你用这些参数调用了函数然后链接器需要找到对应于 header 的代码但他没有找到它是因为在实现中你没有附加类型
(实际上它不止于此,但这是你现在需要知道的,链接器添加了一些额外的参数......)
在这里您可以找到有关此主题的更多信息 http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html
http://www.tenouk.com/ModuleW.html
注意:仅将默认值放在声明中(在 class 内)并且不要在声明外重复它们,实际上现代编译器不接受您编写的内容并分配错误(至少在您使用默认编译参数时在命令行 ) 中,当您将 body 放在 class 之外时,添加参数类型以确保编译器能够找到它。
我一直收到这个错误,我不确定如何解决它。我是初学者,正在尝试构建代码来练习运算符重载。首先,我有错误说存在对 FeetInches(unsigned int= 0, int= 0) 的未识别引用。然后我尝试实现构造函数并得到了那个错误
#include<iostream>
#include<cstdlib>
using namespace std;
class FeetInches
{
private:
unsigned int feet;
int inches;
void simplify();
public:
FeetInches(unsigned int= 0, int= 0);
FeetInches operator+(const FeetInches &);
void display();
};
void FeetInches::simplify()
{
if (inches >=12)
{
feet+= (inches/12);
inches = inches % 12;
}// end of IF
else if (inches <0)
{
feet-=((abs(inches)/12)+1);
inches = 12-(abs(inches)%12);
}// end of elseif
}// end of simplify
FeetInches FeetInches::operator+(const FeetInches & right)
{
FeetInches one (10,15);
FeetInches ruler(5,3);
FeetInches temp;
temp=one+ruler;// temp one.operator +(ruler)
temp.display();
}
FeetInches::FeetInches(feet =0,inches=0)
{
}
void FeetInches::display()
{
cout << "F: " << feet << " I:" << inches << endl;
}
int main()
{
FeetInches aMeasure;
aMeasure.display();
return 0;
};
语法应该是
FeetInches(unsigned int feet = 0, int inches = 0);
用于声明,而不是
FeetInches::FeetInches(unsigned int feet, int inches)
: feet(feet), inches(inches)
{ }
在定义中。
原因是技术上的,而不是逻辑上的,它们取决于 C++ 在较低级别的实现方式。
参数的默认值例如由函数的调用者提供,因此必须在函数的声明中指定函数,因为在 C++ 中,不能保证编译器在编译对函数的调用时知道函数的实现。
很明显,您没有将构造函数 body 附加到您的代码中,为此您可以这样写:
class type_name{
public :
type_name (...ARGS...){ ... }
};
或这个
class type_name{
public :
type_name (...ARGS...);
};
type_name(... ARGS ...){ ...body.. }
所以您需要附加 body 否则链接器不会生成二进制文件,这就是它背后发生的事情:
1- 然后调用编译器为您编写的每个 { .c/.cpp/.cxx ... } 生成一个 .obj 文件。
2- 然后调用链接器来创建最终输出,为此他需要在任何 .obj 文件中为每个调用的函数找到引用。如果您只编写函数声明而不将其称为链接器可能只会发出警告,但如果您调用它,他需要找到它的代码。因为他需要用代码地址替换调用。如果没有代码,他会给你一个未定义的引用错误。
对于每个函数代码,都有一个 header 说明要传递给该代码的参数和类型。他告诉你要找到函数 body 。你在声明 "FeetInches(unsigned int= 0, int= 0);" 中写了 header ,然后你用这些参数调用了函数然后链接器需要找到对应于 header 的代码但他没有找到它是因为在实现中你没有附加类型
(实际上它不止于此,但这是你现在需要知道的,链接器添加了一些额外的参数......)
在这里您可以找到有关此主题的更多信息 http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html
http://www.tenouk.com/ModuleW.html
注意:仅将默认值放在声明中(在 class 内)并且不要在声明外重复它们,实际上现代编译器不接受您编写的内容并分配错误(至少在您使用默认编译参数时在命令行 ) 中,当您将 body 放在 class 之外时,添加参数类型以确保编译器能够找到它。