程序无法识别头文件中的所有函数(除了其中两个)
Program won't recognize all functions from my header file (Except two of them)
我正在实现一个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 ©_from) = default;
更好的是,删除您的operator=
函数,因为您实际上并不需要它 - 编译器将为您生成一个复制所有数据成员的函数,这似乎是您想要的。 那么你也不需要复制构造函数。
此外,您的某些函数签名是错误的。 例如,赋值运算符应为:
money &operator= (const money &);
您的operator+
通常是:
money operator+ (const money &);
这样做将避免不必要的副本。
最后,阅读三/五/零的规则。 在您的情况下,将适用零规则。
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 如何比较两个函数的速度和性能
- 如何在构造器的成员初始值设定项列表中调用两个函数?
- 如何将这两个函数组合成一个实现?
- 将具有相反操作数的两个函数重构为一个
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 具有相同主体的两个函数具有不同的名称
- 通过作为类的公共成员的两个函数将函数作为参数传递
- 如何使用排序和比较这两个函数在 c++ 中对字符数组进行排序?
- 是否可以在C++中将两个函数一起添加
- C++ 两个函数除了一行之外执行相同的操作
- 如何编写连接两个函数的函数
- 如何执行两个函数参数具有相同的模板类型
- 当两个函数位于一行中时,堆栈框架的样子
- 为什么这两个函数的行为不同
- 为什么当我有两个函数时编译器没有显示错误,一个将采用基类,一个将派生类作为参数
- 系统堆栈中的两个函数的递归调用(将不同数量的数组作为参数传递)有什么区别
- 如何在一个构造函数中使用两个可变参数模板参数来绑定两个函数
- 如何在c++中的两个函数中传递对象的同一实例