There are too many problems with this code, some more serious than others. I'll try and point out some of them, but I'm sure I'll miss several.
Data members are public, despite the SetInfo() function
name is just a raw pointer, there's no storage associated with it, so when you assign to it, you're asking for trouble. You should have either a static char array (or dynamically allocate memory) and strcpy to it in setinfo.
Better yet, use a std::string
Also, in main you're passing const char* in name_ instead of char *
A bigger problem is your vector. It's most likely empty and you're indexing into it. You should create your Person objects and then use vector::push_back to add them.
Better yet, write a constructor for your class and use that.
Summarising so far:
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 44 45 46 47 48 49 50 51
|
// Bubblesort.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person
{
public:
Person(const string &_name, int _age) : name(_name), age(_age) {}
//Metod: Sets the necessary info
void SetInfo(const string &_name, int _age)
{
name = _name;
age = _age;
}
const string &Name() const { return name; }
int Age() const { return age;}
private:
string name;
int age;
};
// Function: Main, start on program
int main()
{
//Create a list with persons and fill it:
vector<Person> myList;
cout << "size of myList is " << myList.size() << endl;
myList.push_back(Person("Charles", 22));
myList.push_back(Person("", 0));
//...
// later
myList[1].SetInfo("Darwin", 21);
//...
for (int i = 0; i < myList.size(); ++i)
{
cout << myList[i].Name() << " " << myList[i].Age() << endl;
}
cout << "size of myList is " << myList.size() << endl;
}
|
The prototype of BubbleSort() is incompatible with how it is defined and used
In BubbleSort, p and q are never defined, hence the "give error"
You've not defined a comparison operator for Person, so the comparison in line 91 is illegal, I take it you mean to comapre their ages
Also, for the swap, it's better to use a copy constructor or assignment operator, I've added both and changed swap below
The bubblesort has an off by one error which will eventually crash the program,
the arbitrary max = 9 is unnecessary:
Example without the Linear search:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
// Bubblesort.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person
{
public:
Person() : name(""), age(0) {}
Person(const string &_name, int _age) : name(_name), age(_age) {}
Person(const Person &rhs) : name(rhs.name), age(rhs.age) {}
Person operator=(const Person &rhs)
{
if (&rhs == this)
{
return *this;
}
name = rhs.name;
age = rhs.age;
return *this;
}
//Metod: Sets the necessary info
void SetInfo(const string &_name, int _age)
{
name = _name;
age = _age;
}
const string &Name() const { return name; }
int Age() const { return age;}
private:
string name;
int age;
};
void BubbleSort(vector<Person> &myList);
void swap(Person &p, Person &q);
// Function: Main, start on program
int main()
{
//Create a list with persons and fill it:
vector<Person> myList;
cout << "size of myList is " << myList.size() << endl;
myList.push_back(Person("Charles", 22));
myList.push_back(Person("", 0));
myList.push_back(Person("Michelle", 65));
myList.push_back(Person("Carl", 76));
myList.push_back(Person("Cathrine", 54));
myList.push_back(Person("Marielle", 25));
myList.push_back(Person("Patrick", 85));
myList.push_back(Person("Oliver", 48));
myList.push_back(Person("Elinette", 52));
// later
myList[1].SetInfo("Darwin", 21);
//...
cout << "\nsize of myList is " << myList.size() << endl;
cout << "\nBefore sort:" << endl;
for (int i = 0; i < myList.size(); ++i)
{
cout << myList[i].Name() << " " << myList[i].Age() << endl;
}
BubbleSort(myList);
cout << "\nAfter sort:" << endl;
for (int i = 0; i < myList.size(); ++i)
{
cout << myList[i].Name() << " " << myList[i].Age() << endl;
}
}
void BubbleSort(vector<Person> &myList)
{
// The outer loop, going through all list
for(int i = 0; i < myList.size(); i++)
{
//Inner loop, going through element by element
int nrLeft = myList.size() - i; // To se how many has been gone through
for (int j = 0; j < nrLeft - 1; j++)
{
if (myList[j].Age() > myList[j+1].Age()) // Compare the elements
{
swap(myList[j], myList[j+1]);
}
}
}
}
void swap(Person &p, Person &q)
{
Person temp = p;
p = q;
q = temp;
}
|