Getting a error ...

I need some help here. I have a class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ELECTION_H_
#define ELECTION_H_

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

class election : public base{

	friend ostream& operator<<(ostream&, const election & obj);
        friend istream& operator>>(istream&, election* & obj);
public:
     virtual string what() const;
     virtual void readIn();
     election();
private:
    base* name;
    int** votes;
};

I'm getting a error in the header file saying
std::string* base::name is private.
I'm using this variable within the class.
When switched to
int** votes[][4];
same thing
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
election::election() {
	myname = new base();
	delete [] votes;
	votes = new int*[10];
	for(int i = 0 ; i < 10; ++i )
		votes[i] = new int[4];
}
void election::readIn(){
   cout << "this";
}
string election::what() const{
	return "Wrong input";
}
ostream& operator<<(ostream& os, const election * obj){
	for(int i = 0 ; i < 10; ++i){
		os << obj->myname << " ";
		for (int j = 0; j < 4; ++j)
			os << obj->votes[i][j] << " ";
		os << endl;
	}
	return os;
}
istream& operator>>(istream is , election* obj){

	for (int r = 0; r < 10; ++r) {
		is >> obj->name;
	  for (int c = 0; c < 4; ++c)
		 is >> obj->votes[r][c];
	}
	return is;
}
closed account (Dy7SLyTq)
u need stream& election::operator>> because other wise the function is outside the scope of the class
Maybe 'name' is taken by the base class and declared it private? Try naming your election class' name variable to a different one.
Last edited on
i think you need stream& election::operator....
closed account (Dy7SLyTq)
If u notice guys, the variable in question is declared private
under election. He then tries to use it in a function outside the scope of the class
It should be fine since he put the ostream and istream as friend.

bdub, can you put the ostream and istream functions under the "public:" section.
closed account (Dy7SLyTq)
Oh sorry didn't see that
base::name is private

I really think that means the base class (the class elections inherited from) has its own string called 'name' which is private. So either set that to protected so it can be inherited, or relabel it entirely for election class.
Last edited on
Topic archived. No new replies allowed.