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

标签 c++

我正在尝试使用名为“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/

相关文章:

c++ - 字符串重复由连字符c++替换

C++11 基于范围的 for 循环 : how to ignore value?

c++ - 字符串 vector ,每个字符串的长度和 C++ 中的 strlen

c++ - 如何将 Doxygen 的 "Documentation at other places"功能与专门的模板类一起使用?

c++ - 关于 sizeof 的奇怪之处

c++ - 如何编写指针和数据为 const 的 unique_ptr

c++ - 我的 UI 元素和模型对象应该有什么级别的分离?

c++ - 从另一个线程访问线程本地

c++ - 纠正此错误 :GetLastError 0x13d

c++ - 基于两个过滤器过滤Gtest