首页 > 解决方案 > 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...

标签: c++visual-studio-2017

解决方案


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


推荐阅读