Jump to content
  • Advertisement
Sign in to follow this  
Meta Adam

My program. was(Wierd Errors in this program.)

This topic is 5154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


class person {
private:
string name;
int level;
int hp;
int mana;
int exp;
public:
void set_lvl(int in_level) {
level = in_level;
}
int get_lvl() {
return level;
}
void set_hp(int in_hp) {
hp = in_hp;
}
int get_hp() {
return hp;
}
void set_mana(int in_mana) {
mana = in_mana;
}
int get_mana() {
return mana;
}
void set_exp(int in_exp) {
exp = in_exp;
}
int get_exp() {
return exp;
}
void set_name(string in_name) {
name = in_name;
}
string get_name(){
return name;
}
}

I redid all your code. I think you can learn more from simply reading it.

An example of how you would use this:

person *visage = new person;
visage->set_name("visage");
visage->set_lvl(10000);

cout<<"Visage's name is: " << visage->get_name() << endl;


I would recommend you read some more up on how classes work. A good C++ book would do.

[Edited by - visage on January 6, 2005 5:09:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by visage

class person {
private:
string name;
int level;
int hp;
int mana;
int exp;
public:
void set_lvl(int in_level) {
int level = in_level;
}
void set_hp(int in_hp) {
int hp = in_hp;
}
void set_mana(int in_mana) {
int mana = in_mana;
}

void set_exp(int in_exp) {
int exp = in_exp;
}




I would recommend you read some more up on how classes work. A good C++ book would do.

Why do you have to declare the variables like exp, hp, mana, and exp twice, they are declared in private and then in each of the individual functions.?

Share this post


Link to post
Share on other sites
Okay, I'm getting alot of errors from this:

#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int) {
int personlevel = level;
};
int get_lvl() {
return personlevel;
};
void set_hp(int) {
int personhp = hp;
};
int get_hp() {
return personhp;
};
void set_mana(int) {
int personmana = mana;
};
int get_mana() {
return personmana;
};
void set_exp(int) {
int personexp = exp;
};
int get_exp() {
return personexp;
};
void set_name(string) {
string personname = name;
};
string get_name() {
return personname;
}
};
int main() {
person *dan;
dan->set_lvl(100);
dan->set_name(dan);
cout<<"Here is the person's name: "<<dan->get_name<<endl;
cout<<"Here is his level: "<<dan->get_lvl<<endl;
}


Share this post


Link to post
Share on other sites
Whoo, where to begin. Ill just go through line by ling.


#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
std::string name;
public:


Up till here, your code is fine. RIght here, it gets...ugly. What you need to do is study functions and parameters. Look at your code:

void set_lvl(int) {
int personlevel = level;
};


Okay, so you are getting some integer parameter (with no name...). What are you doing with it? Then you are setting a local variable to be equal to the class variable. Okay...but the local variable is deleted when the method ends...it goes out of scope. What you want to do is this:

void set_level(int in_level){
level = in_level;
}

That will set the class variable "level" to be equal to what was passed to the method.

The same goes for all your input methods.

Your next problem is here:

person *dan;
dan->set_lvl(100);

You never initialize dan. All you have is a pointer. You need to create a cooresponding object. Fixed like this:

person *dan = new person;


Your next issue is here:

dan->set_name(dan);

You are passing a "person" variable into a method expecting a string. If you want the name to be "dan", you need to wrap it in quotes. What you want is:

dan->set_name("dan");

Next, in your cout statements:

cout<<"Here is the person's name: "<<dan->get_name<<endl;
cout<<"Here is his level: "<<dan->get_lvl<<endl;

The error lies in the method calls: you forgot the ()s! It should look like this:

std::cout<<"Here is the person's name: "<<dan->get_name()<<endl;
std::cout<<"Here is his level: "<<dan->get_lvl()<<endl;


It is very, very important that you place:

delete dan

at the end to prevent a memory leak. Whenever you new, you should always delete.

Phew. Hope that helps.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int _level) {
level = _level;
};
int get_lvl() {
return level;
};
void set_hp(int _hp) {
hp = _hp;
};
int get_hp() {
return hp;
};
void set_mana(int _mana) {
mana = _mana;
};
int get_mana() {
return mana;
};
void set_exp(int _exp) {
exp = _exp;
};
int get_exp() {
return exp;
};
void set_name(string _name) {
name = _name;
};
string get_name() {
return name;
}
};
int main() {
person *dan;
dan = new person;

dan->set_lvl(100);
dan->set_name("dan");
cout<<"Here is the person's name: "<< dan->get_name()<<endl;
cout<<"Here is his level: "<< dan->get_lvl() <<endl;

delete dan;

cin.get();
return 0;
}



edit: damn, beaten to it :/

Share this post


Link to post
Share on other sites
Wow thanks! Its still telling me that I have undeclared variables, which is quite wierd, since ive clearly declared them as arguments, heres my new code.

#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int personlevel) {
level = personlevel;
}
int get_lvl() {
return personlevel;
}
void set_hp(int personhp) {
hp = personhp;
}
int get_hp() {
return personhp;
}
void set_mana(int personmana) {
mana = personmana;
}
int get_mana() {
return personmana;
}
void set_exp(int personexp) {
exp = personexp;
}
int get_exp() {
return personexp;
}
void set_name(string personname) {
name = personname;
}
string get_name() {
return personname;
}
};
int main() {
person *dan = new person;
dan->set_lvl(100);
dan->set_name("dan");
cout<<"Here is the person's name: "<<dan->get_name()<<endl;
cout<<"Here is his level: "<<dan->get_lvl()<<endl;
}

Share this post


Link to post
Share on other sites
int get_lvl()
{
return personlevel;
}

"personlevel" needs to be "level", since level is part of your class, not personlevel.


#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int personlevel) {
level = personlevel;
}
int get_lvl() {
return level;
}
void set_hp(int personhp) {
hp = personhp;
}
int get_hp() {
return hp;
}
void set_mana(int personmana) {
mana = personmana;
}
int get_mana() {
return mana;
}
void set_exp(int personexp) {
exp = personexp;
}
int get_exp() {
return exp;
}
void set_name(string personname) {
name = personname;
}
string get_name() {
return name;
}
};
int main() {
person *dan = new person;
dan->set_lvl(100);
dan->set_name("dan");
cout<<"Here is the person's name: "<<dan->get_name()<<endl;
cout<<"Here is his level: "<<dan->get_lvl()<<endl;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!