我的班级没有从其他班级得到正确的信息 [英] My class doesn't get the correct information from the other class
问题描述
Data.h
Data.h
#ifndef DATA_H
#define DATA_H
class Data
{
public:
Data();
void newData();
void saveData();
void getData();
void chgStage();
void display();
float getGold();
int getLvl();
int getStage();
int getExp();
private:
float gold;
int lvl,stage,exp;
};
#endif // DATA_H
Data.cpp
Data.cpp
#include "Data.h"
#include <iostream>
#include "Enemy.h"
#include "Level.h"
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;
Data::Data()
{
}
void Data::newData()
{
fstream flux("data.txt",ios::out);
flux<<"1 "<<"1 "<<"1 "<<"0";
flux.close();
}
void Data::saveData()
{
}
void Data::getData()
{
fstream flux("data.txt",ios::in);
flux>>lvl>>gold>>stage>>exp;
flux.close();
cout<<lvl<<" "<<gold<<" "<<exp;
}
void Data::chgStage()
{
int temp;
cout<<endl<<"You are currently on stage "<<stage;
cout<<endl<<"What stage do you wish to change to?";
cin>>temp;
if(temp==stage)
{
system("CLS");
cout<<endl<<"You are already on this stage\n";
system("PAUSE");
}
else if(temp*10>lvl)
{
system("CLS");
cout<<endl<<"You did not unlock that stage yet.\n";
system("PAUSE");
}
else if(temp!=0)
{
system("CLS");
fstream flux("data.txt",ios::out);
flux<<lvl<<" "<<gold<<" "<<temp<<" "<<exp;
flux.close();
cout<<endl<<"Changed to stage "<<temp<<"\n";
system("PAUSE");
}
}
float Data::getGold()
{
return gold;
}
int Data::getLvl()
{
return lvl;
}
int Data::getStage()
{
return stage;
}
int Data::getExp()
{
return exp;
}
void Data::display()
{
cout<<lvl<<" "<<gold<<" "<<exp;
}
#ifndef LEVEL_H
#define LEVEL_H
class Level
{
public:
Level();
void stage1();
void stage2();
void stage3();
private:
float gold;
int lvl,stage,exp;
};
#endif // LEVEL_H
Level.cpp
Level.cpp
<pre>#include "Level.h"
#include <iostream>
#include "Enemy.h"
#include "Data.h"
#include <windows.h>
using namespace std;
Level::Level()
{
}
void Level::stage1()
{
}
void Level::stage2()
{
}
void Level::stage3()
{
}
我希望将这些私有变量从数据中获取到私有变量,以便在那里使用它们。这也许不是最好的方法,我也会欣赏其他一些更好的方法。
我尝试了什么:
我试图在Level构造函数中创建一个Data对象,但是它没有获取存储在这些变量中的数据(它显示它们就好像它们不是初始化),也试过会员初始化,我可以从主数据中获取私人数据,但我无法从数据到级别等级获取数据。
我' m明显缺少某些东西
I want to get those private var from Data to private var in Level to use them there. Also this is probably not the best way to do this i would also appreciate some other better ways to do this.
What I have tried:
I tried to do an Data object in Level constructor but it doesn't get the data that is stored in those variables(it shows them as if they are not initialized), also tried Member Initializing , i can get the private data in Main from Data but i can't get the data from Data to Level class.
I'm clearly missing something
推荐答案
对于私有成员的访问通常是getter和setter函数标准实现,如下面的代码:
For access of private members normally are getter and setter function the standard implentation like the following code:
//in header
private:
int m_value;// the old hungarian code style of naming
public:
Data(int newValue);
int getValue();
void setValue(int newValue);
//implementation
int Data::getValue()
{
return m_value;
}
void Data::setValue(int newValue)
{
m_value = newValue;
this->m_value = newValue;//some folks like that (I am too old for it)
}
//constructor
void Data::Data(int newValue)
{
m_value = newValue;
this->m_value = newValue;//some folks like that (I am too old for it)
}
这篇关于我的班级没有从其他班级得到正确的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!