gpt4 book ai didi

c++ - 调用提取重载程序产生错误 undefined reference to `operator>>

转载 作者:行者123 更新时间:2023-11-27 23:37:25 24 4
gpt4 key购买 nike

我正在尝试使用名为“Dollars”的类将 float 更改为货币格式。但是当我尝试使用美元类时出现错误。 Dollars 类具有重载提取运算符:

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;

然后我在编译时就得到了这个错误:

对“运算符>>(std::istream&, Dollars&)”的 undefined reference collect2:错误:ld 返回 1 退出状态

最佳答案

您写了 operator <<但你正试图调用 operator >>

关于c++ - 调用提取重载程序产生错误 undefined reference to `operator>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58254247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com