程序无法识别头文件中的所有函数(除了其中两个)

Program won't recognize all functions from my header file (Except two of them)

本文关键字:两个 函数 识别 文件 程序      更新时间:2023-10-16

我正在实现一个money.h头文件,以便重载基本的数学运算符来处理货币对象。该类包括 12 个重载运算符、一个将美元和美分设置为 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 << "nn  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 << "nnnnnn";

// 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 中没有这样的运算符,它应该是钱 + 钱,当它是......

我编译并链接了您的代码,没有警告/错误,只需添加您的主背,删除 pch.h 并删除未定义运算符的使用-->++.

您的主要问题是,由于您有一个用户定义的复制赋值运算符,所以您还应该提供一个用户定义的复制构造函数。 您可以通过将以下内容添加到类定义中来实现这一点:

money (const money &copy_from) = default;

更好的是,删除您的operator=函数,因为您实际上并不需要它 - 编译器将为您生成一个复制所有数据成员的函数,这似乎是您想要的。 那么你也不需要复制构造函数。

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

money &operator= (const money &);

您的operator+通常是:

money operator+ (const money &);

这样做将避免不必要的副本。

最后,阅读三/五/零的规则。 在您的情况下,将适用零规则。