程序无法识别我的头文件中的所有功能(其中两个功能除外)

时间:2019-04-25 18:39:29

标签: c++ visual-studio-2017

我正在实现money.h头文件,以便使基本数学运算符过载以处理money对象。该类包括12个重载运算符,一个将Dollars和cents设置为0的构造函数,一个调用set函数的构造函数,一个set_dollars()和set_cents()函数以及一个get_total()函数。 (set_dollars(),构造函数和get_total是自动内联的)。该程序可以识别重载的赋值运算符和set_cents函数,但是每当我尝试执行操作(例如ob1 + ob2)时,编译器都会完全忽略该语句。

我一直在尝试使用不同的方法来实现头文件并进行研究,但似乎找不到解决该问题的方法。我删除了大多数样式和注释,以使程序尽可能简单。

以下代码显示了头文件“ money.h”中的重载函数的库,类和2。

#include "pch.h"
#include <iostream>
using namespace std;

class money
{
    int   cents,   // Whole number cents amount
          dollars; // Whole number dollars amount

public:

    // Constructor, creates a money amount
    money() {cents   = 0, 
             dollars = 0;}
    money(float amount) {set_dollars(amount),
                         set_cents  (amount); }

    // Sets the dollars and cents values
    void set_cents  (float amount);
    void set_dollars(float amount) {dollars = (int)amount;}


    // Gets the total money amount
    float get_total() {return((float) dollars +
                       (float) (cents * .01f));}

    // Overloaded functions
    money operator=  (money assignment);
   money operator+  (money addition);
   money operator-  (money subtraction);
   money operator*  (money multiplication);
   money operator/  (money division);
   money operator+= (money add_into);
   int   operator== (money equality);
   int   operator!= (money inequality);
   float operator<< (int amount);
   float operator>> (int amount);
   money operator++ ();
   money operator++ (int notused);
};


//*** Sets the cents value of the money amount ***

void money::set_cents(float amount)
{
   // Removes the dollar amount from the total
   amount -= (float) dollars;

   // Rounds the cents to the next whole cent
   if (amount >= 0.00f)
      amount += 0.005f;
   else 
      amount -= 0.005f;

   // Moves the decimal point to make cents a mixed number
   amount *= 100.0f;

   // Truncates the decimal value of cents and stores it into a whole 
   // value of cents
   cents = (int) amount;

   // Checks and distributes the remaining cents and dollars
   if (cents < -99)
   {
      cents   -= 100;
      dollars -= 1;
   }
   else if (cents > 99)
   {
      cents   += 100;
      dollars += 1;
   }

   return;
}


//*** Overloaded assignment operator ***
money money::operator=(money assignment)
{
   dollars = assignment.dollars;
   cents   = assignment.cents;
   return *this;
}

//*** Overloaded addition operator ***

money money::operator+(money addition)
{
   money temp_amount;

   temp_amount.dollars = dollars + addition.dollars;
   temp_amount.cents   = cents   + addition.cents;
   set_cents(temp_amount.get_total());

   return temp_amount;
}

以下代码来自主cpp文件:

#include "pch.h"
#include<iostream>
#include "money.h"
using namespace std;

//*** Main Function ***
   // Print the "complied and linked properly" message
   cout << "\n\n  Your money.h compiled and linked properly, indicating"
        << "\n  all required overloaded operator and member functions"
        << "\n  are present. However this DOES NOT mean the functions"
        << "\n  are working correctly.   YOU MUST completely test all"
        << "\n  your functions yourself, to ensure their reliability.";

   // Normal program termination
   cout << "\n\n\n\n\n\n";


   // The code which follows checks to see if all required money class
   // overloaded operator and member functions are present in the
   // included "money.h" file.

   // Test for the presence of the constructors
   money ob1,        // Uninitialized money object
         ob2(0.00f); // Initialized   money object

   // Test for the presence of the set member functions
   ob1.set_dollars(0.00f); // ERR: Class "money" has no member "set_dollars"
   ob1.set_cents(0.00f);   // ***Works fine***

   // Test for the presence of the get member function
   ob1.get_total(); // ERR: Class "money" has no member "get_total"

   // Test for the presence of the overloaded operator functions
   ob1 = ob2;
   ob1 + ob2; // ERR: No operator "+"  matches these operands. Operand types
   ob1 - ob2;
   ob1 * ob2; 
   ob1 / ob2;  
   ob1 += ob2; 
   ob1 == ob2; 
   ob1 != ob2;
   ob1 << 1;  
   ob1 >> 1;   
   ++ob1;      
   ob1++;      

   cout << ob1.get_total();
   return 0;


}

没有出现编译器错误,并且程序运行正常。每当我尝试执行“ ob1 + ob2”之类的操作并将其输出时,编译器将完全忽略它,并且仍然运行,仅显示标题

操作员用红色下划线标出,表示money.h中没有这样的操作员,应该是money + money,如果是……

2 个答案:

答案 0 :(得分:0)

我通过添加主代码,删除pch.h以及删除未定义运算符--> ++的使用来编译并链接您的代码,而没有任何警告/错误。

答案 1 :(得分:0)

您的主要问题是,因为您有一个用户定义的副本分配运算符,所以还应该提供一个用户定义的副本构造函数。您可以通过将以下内容添加到类定义中来实现:

money (const money &copy_from) = default;

更好的是,删除您实际上不需要的operator=函数-编译器将为您生成一个函数,用于复制所有数据成员,这似乎正是您想要的。然后,您也不需要复制构造函数。

此外,您的某些函数签名是错误的。例如,您的赋值运算符应为:

money &operator= (const money &);

和您的operator+通常为:

money operator+ (const money &);

这样做可以避免不必要的复制。

最后,阅读rule of three/five/zero。在您的情况下,零规则将适用。