尝试从类中打印私有静态整数时出错。 C ++

时间:2018-04-23 03:15:34

标签: c++ logic

我的代码出现问题。每次我尝试从DivSales类打印我的私有静态int total时,程序会运行,但它会打印" 00007FF768191492"有人可以告诉我在我的代码中我做错了什么吗?我在Visual Studios中使用C ++。请注意,我也尝试打印出DivSales :: totalSale;但是在主函数中,我得到了一个类似的输出(程序运行),表示" 00007FF726FF1492"。感谢你们对我的帮助。

 #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.

}

1 个答案:

答案 0 :(得分:4)

这里的代码:

const

打印函数的地址。

要打印函数的返回值,必须先用括号调用它。

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