Solved

Why this endline gives Segmentation Fault (core dumped)

Posted on 2013-05-18
3
332 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Point shot
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now