Class Help (Errors Acessing Data)

I have over 50 classes of information. I want each to have a name, description, and cost inside their respective class. Each time I go to create this array I end up with an error such as: Can't Initialize Data Member. I'm sure it has to do with how and where I am defining the array.

Can I initialize the array variable "description" outside the class and then define it inside each of them? Or does this require a base class and for the others to inherit?

Examples and explanations appreciated.

TY <3
Last edited on
I thought the code would be something similar to this, but anytime I attempt to set the variables equal to something it tells me I don't have access to them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Character{
	public:
		int xp;
		std::string script;
		std::string name;
		void fill(int, string, string);
};

void fill (int xp, string script, string name){

};

class Believer :Character{

};
closed account (D80DSL3A)
You need public inheritance so you can access the Character members in the derived class.
Try:
1
2
3
class Believer : public Character{

};
Thank you, however I'm having trouble understanding how I set and call information from the class. For an example inside State.cpp in my switch(sug) I have:
cout <<" " << believer1.xp << endl;"
The intention is for the experience cost to be output, however the compiler says its undefined. I assume this has something to do with my includes?

database.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef database_H
#define database_H

#include <iostream>
#include <string>
#include "main.h"
using namespace std;

class Character{
	public:
		int xp;
		std::string script;
		std::string name;
		void fill(int, string, string);
};

class Believer : public Character{
	
};

void fill();

#endif 


database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "database.h"

void fill (){ 
	Character believer1, charmedlife1, daredevil1, dreamer1, farmhand1, heiress1, hero1, millitantfamily1, monlisasallure1, prodigy1, scrapper1, tinker1, vibrancy1, wellwornshoes1;
	
	believer1.name = "Believer";
	believer1.xp = 0;
	believer1.script = "";

	charmedlife1.name = "Charmed Life";
	charmedlife1.xp = 0;
	charmedlife1.script = "";

	daredevil1.name = "Daredevil";
	daredevil1.xp = 50;
	daredevil1.script = "Testing";
};


State.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef state_H
#define state_H

#include <iostream>
#include "database.h"

using namespace std;

int sug = 99; //if 99 there is a problem
int background; 
int merit;
void pkstate(char state);
void templ();
void lucid(char state);
void legacy();

#endif 


State.cpp
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
28
29
30
31
32
#include "state.h"

void pkstate(char state)
{
	if (state = 'A'){ //type suggestion
		cout << "Please insert Background suggestion:" <<endl;
		cout << "(type 0 to view possibilities)" << endl;
		cin >> sug;
		cout << " " << endl;

					switch(sug)	//Backgrounds
						{
							case 1: cout << "Believer" << endl;
								cout <<" " << believer1.xp << endl;
								break;
							default: cout <<"Invalid" << endl;
								break;
						}
				cin >> sug;
				}
	if (state = 'B'){ // all random

	}
	if (state = 'C'){ // Archieve INPUT
		templ();
	}
	if (state = 'D'){ // Archieve OUTPUT
	
	}
	else{

	}
bump

just need a quick explanation for why I can't access using:

State.cpp
 
cout <<" " << believer1.xp << endl;" 


Definition is in database.h
fill() sets the variables and is the first thing called in my main.cpp

I believe this has something to do with my includes. =/
It looks like believer1 is local to the function fill()


Are you saying I need to make it global or use a pointer / parameter to pass in info?

Please list example. Thanks =)
closed account (D80DSL3A)
All of the Character objects that you have declared in the fill() exist only in that function. They cannot be accessed anywhere else.
If you need to access these Character objects in your main() then they need to be declared there (or globally).

I suggest replacing your fill() with a constructor for the class.
1
2
3
4
5
6
7
8
class Character{
	public:
		int xp;
		std::string script;
		std::string name;
                // constructor
                Character(int _xp, string _script, string _name): xp(_xp), script(_script), name(_name) {}
};


Then create the Characters in main()
1
2
3
4
5
6
7
8
9
10
int main()
{
    Character believer1( 0, "", "Believer" ), daredevill( 50, "Testing", "Daredevil" );
    // and so on.

    // now call functions like pkstate(), but pass the Character object to it!
    pkstate( 'A', daredevill );

    return 0;
}

The pkstate() would now look 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
28
29
30
31
void pkstate(char state, Character C)
{
	if (state == 'A'){ //type suggestion
		cout << "Please insert Background suggestion:" <<endl;
		cout << "(type 0 to view possibilities)" << endl;
		cin >> sug;
		cout << " " << endl;

					switch(sug)	//Backgrounds
						{
							case 1: cout << C.name << endl;
								cout <<" " << C.xp << endl;
								break;
							default: cout <<"Invalid" << endl;
								break;
						}
				cin >> sug;// why twice?
				}
	if (state == 'B'){ // all random  *** NOTE USE OF == HERE. NOT = ***

	}
	if (state == 'C'){ // Archieve INPUT
		templ();
	}
	if (state == 'D'){ // Archieve OUTPUT
	
	}
	else{

	}
}


Hope that gets you started. Happy coding!
Last edited on
Thank you!
Topic archived. No new replies allowed.