c++ - 尝试从类打印私有(private)静态整数时出错。 C++

标签 c++ logic

我的代码有问题。每次我尝试从 DivSales 类打印我的 private static 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.

}

最佳答案

这里的代码:

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

打印函数的地址。

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

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

关于c++ - 尝试从类打印私有(private)静态整数时出错。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49972915/

相关文章:

c++ - 多个 'const reference' 变量可以共享同一个内存吗?

algorithm - 在递归定义的算法中寻找 g(n)[家庭作业]

swift - 我们如何获得给定月份的星期日数量? ( swift )

c++ - 为什么必须在哪里放置 “template”和 “typename”关键字?

C++ vector 和列表插入

c++ - 在 'if' 语句的条件之间放置代码

c - 编程查找二维数组中的相邻单元格

c - (生命游戏)如何在不检查矩阵外部的情况下循环遍历矩阵的外层,同时检查邻居?

c++ - 物理模拟给出(非常)不准确的简单轨迹微积分位置

c++ - 为什么 QTcpSocket 不写?我需要刷新它才能发送我的数据