简体   繁体   中英

Error when trying to print a private static integer from class. C++

I am having a problem with my code. Everytime I try to print my private static int total from the DivSales class, the program runs however it prints "00007FF768191492" Can someone please tell me what I've done wrong in my code? I am using C++ in Visual Studios. Keep in note that also I tried to print out DivSales::totalSale; in the main function however I got a similar output(the program runs) that says "00007FF726FF1492". Thank you for helping me.

 #include <iostream>
 #include <string>
 #include <iomanip>
 #include <Windows.h>
 using namespace std;


 class DivSales
 {
     private:
         static int total;
     public: 
         int qArr[4];     // here is the declared array I input the 4 integers

     static int totalSale()
     {
         return total;       // here is the function to return total.
     }

     void fourSale(int first, int second, int third, int fourth)       //these integers are inputted by user. 
     {
         if (valid(first) == true)       //this and below is an example of how I am adding to the total variable. Imagine 3 more of these. 
         {
             qArr[0] = first;
             total += first;
         }

 }

int DivSales::total = 0;
int main()
{
     DivSales div1;      //here i declare an object. I have 5 more but I will display two for example purposes. 
     div1.fourSale(6543, 3000, 4000, 5000);      // here i input the 4 integers

     cout << div1.totalSale << endl << endl;       // here I try to print out the total however it prints the error I was talking about.

}

The code here:

cout << div1.totalSale << endl << endl;

Prints the address of the function.

To print the returning value of the function, you must first call it with parentheses.

cout << div1.totalSale() << endl << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM