seg. fault

got segmentation fault for this class

1
2
3
4
5
6
7

istream &operator >> (std::istream &in, const Simple &simple) {
        in >> simple.val;
	return in;
}



The class is:

1
2
3
4
5
6
7
8
9
10
11
12
13

class Simple {
	friend std::ostream &operator << (std::ostream &os, const Simple &simple);
	friend std::istream &operator >> (std::istream &in, const Simple &simple);
public:
	Simple(int val = 0);
	int get() const;
	void set(int val) {this->val = val;}
private:
	int val;
};



Code execution is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

int main() {
	Simple s1(1), s2 = 4, s3, s4;

	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;
	cout << "S3: " << s3 << endl;

	cin >> s4;

	cout << "S4: " << s4 << endl;

return 0;

}



Output works fine but the input doesnt. Thats where my fault is.

Any help would be appreciated.
Last edited on
Remove const from const Simple &simple in operator>>. You're trying to modify something that is declared non-modifiable
const Simple &simple  
in >> simple.val;
It should not even compile. You are trying to change constant variable.
Last edited on
wow i dont know how i missed that basic mistake.

Thanks.
Topic archived. No new replies allowed.