C++ 调用提取重载将生成对“operator”的错误未定义引用>&燃气轮机;
我正在尝试使用一个名为“美元”的类将浮动更改为货币格式。但是当我尝试使用美元类时,我得到了一个错误。美元类具有和重载的提取运算符:C++ 调用提取重载将生成对“operator”的错误未定义引用>&燃气轮机;,c++,C++,我正在尝试使用一个名为“美元”的类将浮动更改为货币格式。但是当我尝试使用美元类时,我得到了一个错误。美元类具有和重载的提取运算符: istream & operator >> (istream & in, Dollars & rhs) { // initially zero rhs.cents = 0;
istream & operator >> (istream & in, Dollars & rhs)
{
// initially zero
rhs.cents = 0;
if (in.fail())
return in;
// skip leading spaces and dollar signs;
while (isspace(in.peek()) || in.peek() == '$')
in.get();
// is the next character a negative?
bool negative = false;
while ('-' == in.peek() || '(' == in.peek())
{
negative = true;
in.get();
}
// consume digits, assuming they are dollars
while (isdigit(in.peek()))
rhs.cents = rhs.cents * 10 + (in.get() - '0');
// everything up to here was dollars so multiply by 100
rhs.cents *= 100;
// did we get a decimal
if ('.' == in.peek())
{
// consume the decimal
in.get();
// next digit is in the 10cent place if it exists
if (isdigit(in.peek()))
rhs.cents += (in.get() - '0') * 10;
// the final digit is the 1cent place if it exists
if (isdigit(in.peek()))
rhs.cents += (in.get() - '0');
}
// take care of the negative stuff
rhs.cents *= (negative ? -1 : 1);
// see if there is a trailing )
if (')' == in.peek())
in.get();
return in;
}
以下是我尝试使用它的地方:
Dollars dollar;
cout << "Float to convert to Dollars: ";
cin >> dollars;
美元;
美元;
当我编译时,我得到了这个错误:
对“operator>>(std::istream&,Dollars&)的未定义引用
collect2:错误:ld返回了1个退出状态您编写了
运算符您编写了运算符抱歉,该类同时包含了两个运算符我复制了正确的运算符>>现在您正在编译并链接包含函数定义的文件吗?抱歉,我复制了正确的运算符>>现在您是否正在编译和链接包含函数定义的文件?使用运算符的代码不完整。(1) 没有证据表明运算符>>()
的类定义或声明在使用时对编译器可见。两者都是必不可少的。(2) “使用”运算符的代码不在函数中,因此不可编译。(3) 如果操作符>>()
的定义和使用它的函数在不同的源文件中,那么关于如何构建(编译和链接)的信息与回答问题相关。您没有提供允许他人帮助所需的任何信息。请仔细阅读提供的。使用运算符的代码不完整。(1) 没有证据表明运算符>>()
的类定义或声明在使用时对编译器可见。两者都是必不可少的。(2) “使用”运算符的代码不在函数中,因此不可编译。(3) 如果操作符>>()
的定义和使用它的函数在不同的源文件中,那么关于如何构建(编译和链接)的信息与回答问题相关。您没有提供允许他人帮助所需的任何信息。请仔细阅读关于提供一个。