Help with accessor error!

Hello, guys!

I really need help on this one because I have no idea why it won't recognise an object I created... so here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include "Character.h"
using namespace std;

void start();
void printCharacterStats();

int main() {

	//some code
	
	return 0;
}

void start() {
        //some code again...
	string name;
	cin >> name;
	Character Hero;
	Hero.setCharacterName(name);
        //bla bla bla
}

void printCharacterStats() {
	cout << Hero.getCharacterName(); //now HERE is where the error is
}


it highlignts "Hero" and sais: "Error: identifier "Hero" is undefined"
so what would the problem be? becauze I created the object Hero, I stored in it a name using a mutator, I can also print the stored name using an accessor (only in that "start" function, but it won't recognise it in the "printCharacterStats" function! so please help me, tell me where I messed up because I'm a beginner :-<

btw tell me if you need the class code too, or some of it...
Last edited on
closed account (Dy7SLyTq)
cause its a local object created in the start function. it cant be used outside of it
@DTSCode thanks, man! I've created the object as a global object and it works now! ^_^ also thank you for the fast reply :D
closed account (Dy7SLyTq)
no problem
closed account (3qX21hU5)
Also just a suggestion but I would learn about function parameters and return values. Ideally you shouldn't have Objects like that in the global namespace.
@Zereo so what you're saying is... it would be better if I create the object at the beginning of the main function?

EDIT: I tried it, it gives a lot of errors. it's like I never created it :-s so I'll leave it global. if you're sure it's wrong, please tell me a better way :D
Last edited on
closed account (3qX21hU5)
It should not be global I am sure of that, though you must understand that you must learn about scopes and how to use functions. You aren't using functions right if everything you are changing with the function is a global variable. You should be passing variable by reference and by value into functions and returning or not returning your results.

Check out the function tutorial on this site to learn more. But here is a quick example. (I wouldn't really use functions like this but this is a example)

Lets say I had a struct called "Player" it held all my stats for a player in my game.

1
2
3
4
5
struct Player {
    std::string name;
    int attack;
    int health;
};


Now lets say I want to have a function that sets up the stats for the player (Like health, name and attack) that I have just created. I might do something like this.

1
2
3
4
5
void setUpPlayer(Player &newPlayer) {
    newPlayer.name = "Brandon";
    newPlayer.attack = 15;
    newPlayer.health = 100;
}


I would then call this function like setUpPlayer(brandon); // Where brandon is the objects name we created .

What this function does is sets up the stats for the new player object we created by using a reference which mean we directly alter whatever object we pass into the function as a argument.

The whole thing would look maybe something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>

struct Player {
    std::string name;
    int attack;
    int health;
};

void setUpPlayer(Player &newPlayer) {
    newPlayer.name = "Brandon";
    newPlayer.attack = 15;
    newPlayer.health = 100;
}

int main() {
    // Creates my new object
    Player brandon;

    // Calls my function to setup the stats of my player
    setUpPlayer(brandon);

    // Just shows what all the members of the object are set to
    std::cout << "Name: " << brandon.name << std::endl;
    std::cout << "Attack: " << brandon.attack << std::endl;
    std::cout << "Health: " << brandon.health << std::endl;
}


Now this is not the best way to create stuff like this and you should instead use classes to handle these things but I just wanted to show how you can use functions without having every objects or variable that the function alters have to be in the global namespace. So go checkout the functions tutorial it is actually very helpful and functions are very powerful.


Also sorry if there are mistakes in the code ;p have been coding in Python for the last few months almost exclusively. And if you need any help with anything chippz just let me know I would be glad to help answer any questions you have.
Last edited on
I'm glad you offered to help me if I have any more questions ^_^ thank you!

but uhm... actually I WAS using classes :D it's in the original post (at the end). and frankly, this is the second time I've heard about "struct" xD
as I can see it is another way to create objects instead of using classes, am I correct? can you please tell me more about "struct", please?

EDIT: btw I also use functions like void function() to "extend" the main function, for the orderliness' and cleanliness' sake. it doesn't have to return anything or include/modify any argument :D
Last edited on
closed account (3qX21hU5)
I've heard about "struct" xD
as I can see it is another way to create objects instead of using classes, am I correct? can you please tell me more about "struct", please?


You are correct a struct is another way of creating a object in C++. Basically the struct and class keyword do the same exact thing except for one difference. That difference is that struct is public by default and class is private by default.

By this I mean

1
2
3
4
class MyObject {
    public:
        // You stuff here
};


and

1
2
3
struct MyObject {
    // Your stuff here
};


are the same thing.

btw I also use functions like void function() to "extend" the main function, for the orderliness' and cleanliness' sake. it doesn't have to return anything or include/modify any argument :D


You are true functions don't have to return anything or include/modify anything but they usually should. My rule of thumb generally is if I am only going to use the function once in my program it really doesn't need to be a function (Though obviously there is exceptions to this).

And if you are only using void function that don't alter anything or return anything you are only using about 5% of functions full potential.
thank you for the information about "struct" ^_^

now you're telling me more than I asked xD

I know you can do a lot with functions and the main use of a function is that if you know you'll write the same code multiple times in the future, it's easier to write a function and just call it when you need it instead of copy/pasting the code over and over again and maybe copying it wrong.
thank you, but I already know of functions :D and their arguments and stuff xD thank you anyway ^_^
Last edited on
Topic archived. No new replies allowed.