1

每当我尝试编译程序时,我都会遇到编译错误。

当我尝试删除该程序中的“排序”功能时,一切都很好,当我使用“排序”功能时开始出现问题。

有没有其他方法可以解决这个问题,或者我无法使用 #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'

我试过运行我在网上找到的这个例子(下面附上的源代码),我也面临同样的问题?为什么会这样?

http://www.daniweb.com/software-development/cpp/threads/242984/sorting-multiple-array-containers-with-related-elements#post1064721

4

3 回答 3

2

您需要包含字符串头文件:

#include <string>

也让 const 引用到tripical

bool sortByName(const tropical &t1, const  tropical  &t2);

查看实时样本。

于 2013-10-25T11:03:19.967 回答
1

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;
}
于 2014-07-06T07:59:57.167 回答
0

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;
}
于 2013-10-25T11:04:44.673 回答