473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

segmentation when cout

20
Hi,

I'm trying to overload the << operator. I have a point class which is represented by a vector of doubles. In this class I have overloaded the << operator with no problem:
Expand|Select|Wrap|Line Numbers
  1. class Point
  2. {
  3.   public:
  4.     //constructor
  5.     Point();
  6.     Point(vector<double> v);
  7. }
  8.  
  9. Point::Point()
  10. {
  11.    vector<double> vec(4,0);
  12.    vec[3]= 1;
  13.    point = vec;
  14.    size  = vec.size();
  15. }
  16.  
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, const Point& p) 
  2. {
  3.    int sz = p.get_size();
  4.    os << "(";
  5.    for (int i=0; i<sz; i++) {
  6.       os << p.get_val(i) << " ";
  7.    }
  8.    os << ")" << endl;
  9.    return os;
  10. }
Now I have created another class, which instantiates 4 points of class Point:

Expand|Select|Wrap|Line Numbers
  1. fourpoints::fourpoints()
  2. {
  3.     Point p1;
  4.     Point p2;
  5.     Point p3;
  6.     Point p4;
  7. }
When I try to overload the << operator here I get a segmentation fault:
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, const fourpoints& p) 
  2. {
  3.    os << "1. point: " << p.get_1();
  4.    return os;
  5. }
The function get_1() just returns the first point:
Expand|Select|Wrap|Line Numbers
  1. Point fourpoint::get_1() const
  2. {
  3.     return this->p1;
  4. }
Can anybody tell what I'm doing wrong? It fails when I try to print the coordinate in the point. The output is "1. point: (" and nothing more
Apr 18 '08 #1
5 1208
weaknessforcats
9,208 Expert Mod 8TB
fourpoints::fourpoints()
{
Point p1;
Point p2;
Point p3;
Point p4;
}
These Point variables are local to this constructor. They will be destroyed when the constructor completes.

You didn't post your fourpoints class so I can't see what is decared there.
Apr 18 '08 #2
madshov
20
The fourpoints class:
Expand|Select|Wrap|Line Numbers
  1. class fourpoints
  2.     public:
  3.  
  4.     //constructor
  5.     fourpoints();
  6.  
  7.     //destructor
  8.     ~fourpoints();
  9.  
  10.     Point fourpoints::get_1() const;
  11.     friend ostream& operator<<(ostream& os, const fourpoints& p);
  12.  
  13.     private:
  14.     Point p1, p2, p3, p4;
  15. };
Apr 18 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Point::Point()
{
vector<double> vec(4,0);
vec[3]= 1;
point = vec;
size = vec.size();
}
Your vector vec is local to the Point constructor. It will be destroyed when the constrcutor completes.

This is the second place you have done this. Your data members belong in the class as private members and not as local variables in your funcitons.

Also, vec[3] will fail becuse there is no element there. You need to put elements in the vector before you can assign to them.

Also, vector<double> vec(4,0) won't compile on my system.

Also, point and size are not defined so these lines won't compile either.


I suggest you fix your code so it compiles and post again.
Apr 18 '08 #4
Your vector vec is local to the Point constructor. It will be destroyed when the constrcutor completes.

This is the second place you have done this. Your data members belong in the class as private members and not as local variables in your funcitons.

Also, vec[3] will fail becuse there is no element there. You need to put elements in the vector before you can assign to them.

Also, vector<double> vec(4,0) won't compile on my system.

Also, point and size are not defined so these lines won't compile either.


I suggest you fix your code so it compiles and post again.

The two parameter constructor is defined here: http://cppreference.com/cppvector/vector_constructors.html

If you left out any code post it.

Max
Apr 18 '08 #5
madshov
20
Hi,
Sorry, I only pasted some parts of my code, but I found the reason for the problem myself.
Apr 21 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
1
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std;...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
4
by: fantasticamir | last post by:
here is my code!! #include <pthread.h> #include <iostream> #include <unistd.h> #include <map> #define MET_TIMEOUT_INTERVAL 10;
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
6
drumgirl67
by: drumgirl67 | last post by:
I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total...
0
by: wmihelpme | last post by:
Hi All, I have written a WMI Program which changes the network settings like ip address, subnet mask, gateway and dns addresses for given adapter number. when I call that function for adapter...
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
10
by: sagitalk | last post by:
Here is my code: #include <iostream> #include <vector> #include <fstream> #include <stdio.h> #include <string> #include <cstring> using namespace std;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.