プログラムをコンパイルしようとすると、コンパイル エラーが発生します。
このプログラムで「並べ替え」機能を削除しようとすると、すべて問題ありませんが、「並べ替え」機能を使用すると問題が発生します。
この問題を解決する他の方法はありますか、または #include を使用する方法はありませんか? 「スワップ」機能を使用する場合も同様です。
ここにコードがあります
#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;
}
これは私が得ているエラーです
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'
Web で見つけたこの例 (以下にソースを添付) を実行してみましたが、同じ問題に直面していますか? これはなぜですか?