Solved

Why this endline gives Segmentation Fault (core dumped)

Posted on 2013-05-18
3
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

740 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