Seg fault on overload << operator

I'm writing a simple Cell class for a sudoku game, and my overloaded extraction operator is giving me a segmentation fault whenever I use it. Was hoping someone here may be able to help me isolate the issue, as I've checked my code several times over now and cannot seem to figure out what's wrong.

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
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
using namespace std;

class Cell {
private:
	bool readOnly;
	int value;
public:
	Cell(int value, bool readOnly) {
		this->value = value;
		this->readOnly = readOnly;
	}

	bool isReadOnly() {return readOnly;}
	int getValue() const {return value;}
	void setValue(int value) {if (!readOnly) this->value = value;}
//	ostream& operator << (ostream& out, const Cell &rhs);
};

ostream& operator << (ostream &out, const Cell &rhs) {
	out << " " << rhs.getValue() << " ";
	
	return out;
}

void getFilename(char* filename) {
	cout << "Enter source file: ";
	cin >> filename;
}

int main() {
	char* filename;
	getFilename(filename);
	
	//Board *board = new Board(filename);

	Cell cell(0, true);
	cout << cell << endl;
	//delete board;
	return 0;
}


It seems to only seg fault with the endl, but I'm sure it must be something with my code.
This code seems to work just fine.
No segfaults at any of these online compilers:
http://cpp.sh/5ljr
http://ideone.com/I3UYLs
http://coliru.stacked-crooked.com/a/fc661339b8033df8
All I did was comment out line 34, so that I did not have to type any standard inputs.

So this leads me to the question: What compiler are you using?


The code worked fine because I commented out the line causing the segfault. Change line 33 to char filename[50];
This will allocate memory, instead of having a pointer to wonderland.
Last edited on
Thanks norm. I overlooked that.
OP's been around a year and a half so should know better.
Topic archived. No new replies allowed.