调用提取重加载器会产生对"运算符"的未定义引用错误>>
Calling the extraction overloader produces error undefined reference to `operator>>
我正在尝试使用一个名为"美元"的类将浮点数更改为货币格式。 但是当我尝试使用美元类时,我遇到了错误。美元类具有和重载提取运算符:
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&, Dollar&("的未定义引用 collect2:错误:ld 返回 1 个退出状态
你写了operator <<
但你试图调用operator >>
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 是否需要使用 - &gt;运算符在C 中调用成员函数时