Hi,
I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the x-coordinate. I am trying to use the "sort" function defined in algorithms.h and I am getting a segmentation fault. I have inlined the code below. Can someone please tell me what I am doing wrong ? I am compiling this code on linux ( fedora 6 x86_64 using gcc 4.0)
-
Many thanks
JWalker
PS: I am just learning C++ and STL perhaps I am making some fundamental mistake.
=============== =============== =============== ==========
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class _vertex{
public:
double x;
double y;
double z;
_vertex() {x=0.0, y =0.0; z =0.0;}
_vertex(double xa,double ya, double za){x=xa; y =ya; z=za;}
_vertex (const _vertex & b) {x = b.x; y=b.y; z=b.z;}
};
bool sortbyx(const _vertex& v1,const _vertex& v2){
if(v1.x<=v2.x)
return true;
else
return false;
}
void print(const _vertex& tmp){
cout<<tmp.x<<"\ t"<<tmp.y<<"\t" <<tmp.z<<"\t"<< endl;
}
int main(){
vector<_vertex> nodes;
// read node information from files in vector "qvolist"
vector<string> qvolist;
qvolist =Read_qvofile_l ist("temp.txt") ;
for(int i =0; i<qvolist.size( );i++){
ifstream qvofile;
string line;
int Nvertices, NFaces, NEdges;
qvofile.open((q volist.at(i)).c _str());
cout<<(qvolist. at(i))<<endl;
if(qvofile.is_o pen())
{
getline(qvofile ,line); // read dimensions dummy var
qvofile>>Nverti ces>>NFaces>>NE dges;
_vertex tmp;
for(int j =0; j<Nvertices; j++){
qvofile>>tmp.x> >tmp.y>>tmp.z ;
nodes.push_back (tmp);
}
qvofile.close() ;
}
else cout<<"unable to open qvo file"<<(qvolist .at(i)).c_str() <<endl;
}
// print list of vertices
for_each(nodes. begin(), nodes.end(),pri nt);
//Sort vertices by x co-ordinate
sort(nodes.begi n(), nodes.end(),sor tbyx);<-----------segmentation fault here
=============== =============== =============== =============
1 3049
The problem with your code is almost certainly with the part concerned with reading data from the file (or its a compiler bug). Without a definition for Read_qvofile_li st, I can't tell if it's in there or not. I did notice, however, that you make no check for end-of-file in your for loop; that is bad practice, for if you opened a file that was too short, your program wouldn't behave as expected. This may be the source of your segfault; I have found that programs rarely segfault where the actual problem was.
As well, it is generally good C++ practice to have all data members private, and declare member functions to get and set them, not that this has any relavance to the segfault.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: diyanat |
last post by:
i am writing a cgi script in C using the CGIC library, the script
fails to run, i am using apache on linux
error report from apache : internal server error
Premature end of script headers: /var/www/cgi-bin/script.cgi
when i debug the program i get Segmentation fault
gdb ./script.cgi
|
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 WORDS_PER_LINE = 4;
when counter == 7 is when the string Concatenation fails within the IF
block.
|
by: sandwich_eater |
last post by:
I get a segmentation fault in my program when calling a function
"TestFn" that has been passed as a pointer into another function.
The following excerpt should give enough information as to what I am
doing wrong...
typedef void TIdxMultiFunc (int i, std::vector<double> f);
void TestFn(int i, std::vector<double> f)
{
double t; // execution does not get this far
|
by: Fra-it |
last post by:
Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I understood that a segmentation
fault can occur whenever I declare a pointer and I leave it
un-initialized.
So I thought the problem here is with the (const char *)s in the stuct
flightData (please note that I get the same fault declaring as char *
the...
|
by: samuel.y.l.cheung |
last post by:
Hi,
I wrote a template to use copy() algorithm, called copyAll:
template<class T>
void copyAll(const T& src , T& dest ) {
copy (src.begin(), src.end(), back_inserter(dest));
}
but when I call it, I get a segmentation fault:
Thread (Suspended: Signal 'SIGSEGV' received. Description:
| |
by: utab |
last post by:
Dear all,
I tried sth like this, compiles but segmentation fault error. In my
reasoning field_values holds a vector<double> but when I tried, I
understood that it is not the case :-).
#include <iostream>
#include <vector>
using namespace std;
|
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 include all of it-
it's over 1k lines long). In addition, I'm including an "include"
file where structures like "stack" are defined. Again, it's really
long. I doubt the problem lies there, though, because the include
file is used in many other...
|
by: Steve |
last post by:
I have segmentation fault when calling resize on an stl vector. I'm
unsure if I'm doing something horribly wrong or if the stl is being
dumb. The code breaks down like this:
-------------------------------------
class Device {
public:
Device() {
m_isBusy = false;
|
by: RLC |
last post by:
Hello
I am new to python SWIG. Recently I wrote a small program trying to
import collada files(using colladadom) into python so I could use
python cgkit to render them. However, during the progressing, I got
some problems. Every time I quit from Python, I get a segmentation
fault, although the main program runs well. I suspect it is because I
wrapped std::vector objects in C struct and I did not release the
memory properly. Below is the...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |