c++ - Program won't recognize all functions from my header file (Except two of them)
问题描述
I am implementing a money.h header file in order to overload basic mathematical operators to work on money objects. The class includes 12 overloaded operators, a constructor that sets dollars and cents to 0, and one that calls the set functions, a set_dollars() and set_cents() function, and a get_total() function. (The set_dollars(), constructors, and get_total are auto-inlined). The program recognizes the overloaded assignment operator, and the set_cents function, but any time I try to perform an operation (such as ob1 + ob2), the compiler completely ignores the statement.
I have been trying different methods of implementing the header file, and doing research, but can't seem to find a fix to the problem. I have removed the majority of style and comments to keep the program as simple as possible.
The following code shows the libraries, class, and 2 of the overloaded functions in the header file "money.h".
#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;
}
Following code is from main cpp file:
#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;
}
No compiler errors show up, and the program works fine. Whenever I try to do an operation such as "ob1 + ob2" and output it, the compiler completely ignores it and still runs showing only the heading
The operators are underlined in red and say that there is no such operator in money.h, and that it is supposed to be money + money, When it is...
解决方案
我编译并链接了您的代码,没有任何警告/错误,只需添加您的 main back、删除 pch.h 并删除未定义运算符的使用-
-> ++
。
推荐阅读
- php - 表格Vue组件上没有数据返回
- c++ - 加载时 Windows dll 插件崩溃
- java - Java Spring Bean 依赖注入,conditionalOnBean
- php - 如何访问 PHP 数组中的不同键和值
- python-3.x - 将输入文件发送到使用 getpass 函数的 python 代码
- javascript - 如何防止 mousemove 事件在触摸设备中作为点击工作?
- php - 就我的表单无法正确验证的原因请求帮助
- python - Twitter API - 请求延迟问题 - 随机延迟?- Python 请求
- postgresql - 在 PostgreSQL 9.4.1 的切换和切回中是强制参数recover_target_timeline='latest'吗?
- javascript - 通过ajax发送Indside ul数据