类函数给出垃圾值

class function gives garbage value

本文关键字:类函数      更新时间:2023-10-16

我是c++的新手。我创建了名为Cache的类,并使用构造函数创建了一个对象L1Cache。我正在将参数传递给构造函数,并正确打印计算数据。但当我在同一个类和同一个对象中调用不同的函数时,我在构造函数中生成的数据会给出垃圾值。在这里,我想提到的是,我的所有函数和变量都是公共的。

我的代码看起来像这样:

main.cc:
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "cache.h"
main()
{
    unsigned long long Address= 5555555555;
    unsigned long a=5,b=7;
    Cache L1Cache(a, b);
    L1Cache.Calculate( Address);
}
cache.cc:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <string.h>
#include <stdlib.h>          
#include "cache.h"
using namespace std;
Cache::Cache(unsigned long c,unsigned long d)
{
    e= c+d;
    printf("Value Of e:%lu",e);
}
void Cache::Calculate(unsigned long long A)
{
    printf("Value of e:%lu",e);
}
cache.h:
class Cache
{
      public: 
      unsigned long e;
      Cache(unsigned long c,unsigned long d)
      void Calculate(unsigned long long A);
}

输出:

Value of e: =12;
Value of e: = garbage

类声明中和之后缺少分号。main应该是int。在修复代码运行后。你真的运行了你认为运行的代码吗?