Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why this endline gives Segmentation Fault (core dumped)

Posted on 2013-05-18
3
Medium Priority
?
340 Views
Last Modified: 2013-05-19
ostream& operator<<(ostream& stream, Network& network)
	{
		int a;
		int s = network.nodes.size();
		for (a=0;a<s;a++)
		{
				stream << *(network.nodes[a]);	
		}
	}

ostream& operator<< (ostream& stream, const Node& node)
{
		stream << node.label ;
}


int main()
{
Node * n = new Node("W");
cout << *n ; // IT WORKS
cout << *n <<endl; // IT WORKS

Network * x = new Network();
x->addNode("A");
cout << *x // IT WORKS
cout << *x << endl // THE endl causes segmentation fault  (core dumped). WHY??What should i do?

	
}

Open in new window

Any help is appreciated.
0
Comment
Question by:codeBuilder
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 39178066
Your operator is not returning the stream instance, and I have to say I am quite surprised that this compiled without errors or warnings - it should be

ostream& operator<<(ostream& stream, Network& network)
	{
		int a;
		int s = network.nodes.size();
		for (a=0;a<s;a++)
		{
				stream << *(network.nodes[a]);	
		}

                return stream; // <-- this one is important
	}

Open in new window

0
 

Author Comment

by:codeBuilder
ID: 39178325
OH NO! How bad i forgot it :(
In addition , i am surprised now how it can compile correctly , but maybe it won't give any warning because i don't state any extra option while compiling . I compile as the following: g++ -o example example.cpp
0
 

Author Closing Comment

by:codeBuilder
ID: 39178333
Point shot
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question