类函数给出垃圾值
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。在修复代码运行后。你真的运行了你认为运行的代码吗?
相关文章:
- 如何通过派生类函数更改基类中的向量
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 在类函数中初始化外部作用域变量
- c++ 在非类函数中使用类变量
- SDL_PollEvent() 无法捕获类函数内部SDL_QUIT?
- 从类成员函数到类 C 函数指针的转换
- 如何在模板类函数中分配结构值?
- 有没有办法将重载的类函数绑定到函数对象?
- 启动类函数作为失去引用的线程
- C++调用使用重写函数的父类函数
- 将值传递到另一个类函数在打印时为零
- 计算对类函数的所有调用次数
- 由于签名差异,调用了错误的子类函数
- 使用宏调用类函数
- 虚拟基类函数中派生类的大小
- 模板类/函数中的交叉前向声明
- 在C++中使用非静态类函数的函数
- 无法访问基类函数 C++
- 类函数返回到另一个类函数返回打印不同的值
- C++ 在外部使用嵌套结构实现类函数