First of all hello to everyone.
I am trying to create a small program that does the following: stores a series of aircraft types (class acfttype in acfttype.cpp) inside a list (class listacfttype in listacfttype.cpp).
Acfttype works flawlessly, I have tested it with no problem. The problem is when I have created the listacfttype overriding "operator<<". The program compiles and links fine, but when I try to execute it I get a "Segmentation fault" error.
Here's the code for acfttype:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#ifndef ACFTTYPE
#define ACFTTYPE
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
class acfttype {
private:
string model;
public:
acfttype() {}
acfttype(string s) { model=s; }
acfttype(const acfttype &at) { model=at.gettype(); }
~acfttype() {}
void settype(string s) { model = s; }
string gettype() const { return model; }
friend ostream& operator<<(ostream &out, acfttype &at) {
out << at.model << endl;
return out;
}
friend istream& operator>>(istream &in, acfttype &at) {
string temp;
cout << "Type model: ";
getline(in, temp);
transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
at.model = temp;
return in;
}
friend bool operator==(const acfttype &at1, const acfttype &at2) { return at1 == at2; }
friend bool operator!=(const acfttype &at1, const acfttype &at2) { return at1 != at2; }
friend bool operator<(const acfttype &at1, const acfttype &at2) { return at1 < at2; }
friend bool operator>(const acfttype &at1, const acfttype &at2) { return at1 > at2; }
friend bool operator<=(const acfttype &at1, const acfttype &at2) { return at1 <= at2; }
friend bool operator>=(const acfttype &at1, const acfttype &at2) { return at1 >= at2; }
};
#endif
| |
And here's the code for listacfttype:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#ifndef LISTACFTTYPE
#define LISTACFTTYPE
#include <iostream>
#include <string>
#include <list>
#include <cctype>
#include <stdlib.h>
#include "acfttype.cpp"
using namespace std;
class listacfttype {
private:
list<acfttype> lst;
public:
listacfttype() {}
~listacfttype() {}
void addtype (string &s) {
lst.push_back(s);
}
void orderlistacfttype() { lst.sort(); }
void removeduplicates() { lst.unique(); }
friend ostream& operator<<(ostream &out, listacfttype <) {
int i=0;
i++;
return out;
}
friend istream& operator>>(istream &in, listacfttype <);
};
#endif
| |
The class listacfttype works fine, but as soon as I add the "operator<<" begins to fail. As you can see for now the only thing I'm doing inside "operator<<" is declaring an int and ++, but I get the "Segmentation fault" error, can anyone enlighten me?
Just in case: I'm programming in linux, but I assume this to be more of a general programming problem, it it's not the case please forgive me.