Reputation: 67
I'm having compilation error whenever i try to compile the programme.
when I try to remove the "sort" function in this programme everything is fine, problem start arising when I use the "sort" function.
Is there any other way to solve this issue or there no way i could use #include ? It's the same when I use the "swap" function.
here's the code
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <vector>
#include <string>
using namespace std;
class tropical
{
public:
string name;
int price;
};
bool sortByName(tropical &t1, tropical &t2); //
void displayfruits(vector<tropical> fruitlist)
{
cout << "Name \t\tPrice" << endl << "==========\t=====" << endl;
for(int i=0;i<10;i++) //displays all fruits' names and prices
{
cout << fruitlist[i].name << " \t" << fruitlist[i].price << endl;
}
cout << endl;
}
int main()
{
int sortchoice;
string searchfruit;
string fruitname[] = {"Avocado", "Papaya", "Grapefruit", "Pineapple", "Jackfruit", "Orange", "Honeydew", "Mangosteen", "Banana", "Durian"};
int fruitprice[] = {1, 4, 6, 2, 10, 3, 9, 7, 5, 8};
vector<tropical> fruitlist;
tropical fruit;
vector<tropical>::iterator it;
for(int i=0; i<10; i++)
{
fruit.name = fruitname[i];
fruit.price = fruitprice[i];
fruitlist.push_back(fruit);
}
displayfruits(fruitlist);
sort (fruitlist.begin(), fruitlist.end(), sortByName);
displayfruits(fruitlist);
}
bool sortByName(tropical &t1, tropical &t2)
{
return t1.name < t2.name;
}
this is error i'm getting
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/algorithm:63:0,
from ..\src\tutorial 3 - Constants and Formatting Decimals.cpp:2:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<tropical*, std::vector<tropical> >, _Tp = tropical, _Compare = bool (*)(tropical&, tropical&)]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2265:78: instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<tropical*, std::vector<tropical> >, _Compare = bool (*)(tropical&, tropical&)]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2306:62: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<tropical*, std::vector<tropical> >, _Size = int, _Compare = bool (*)(tropical&, tropical&)]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5445:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<tropical*, std::vector<tropical> >, _Compare = bool (*)(tropical&, tropical&)]'
..\src\tutorial 3 - Constants and Formatting Decimals.cpp:56:61: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2233:4: error: invalid initialization of reference of type 'tropical&' from expression of type 'const tropical'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2236:4: error: invalid initialization of reference of type 'tropical&' from expression of type 'const tropical'
I've tried running this example which i've found on the web (the source attached below) and i'm facing the same problem? why is this so?
Upvotes: 1
Views: 5234
Reputation: 445
You must modify both the declaration and definition of function sortByName to receive const references to tropical, like so:
bool sortByName(const tropical &t1, const tropical &t2); //Prototype (declaration)
bool sortByName(const tropical &t1, const tropical &t2) //definition
{
return t1.name < t2.name;
}
Upvotes: 1
Reputation: 1475
Try to define args of sortByName as const.
// Declaration
bool sortByName(const tropical t1, const tropical t2);
// Implementation
bool sortByName(const tropical t1, const tropical t2)
{
return t1.name < t2.name;
}
Upvotes: 0