【问题标题】:c++ compilation error using #include<algorithm>c++编译错误使用#include<algorithm>
【发布时间】:2013-10-25 11:00:38
【问题描述】:

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

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

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

【问题讨论】:

  • 在我的编译器上编译得很好。

标签: c++ algorithm compiler-errors


【解决方案1】:

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

#include <string>

同时对tripical进行const引用

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

参见live 示例。

【讨论】:

  • 是的,它已经包含在内,但我在编译时仍然遇到问题。
  • 对不起,因为我是 C++ 新手,所以请原谅我的愚蠢问题。为什么需要进行 const 引用?
  • @user2900611 因为这是 sort() 函数所期望的。 sort() 会将 const 引用传递给比较函数。所以 sortByName 不应该能够修改被比较的数据 - 如果这样做了,那么订单最终将是不可预测的。
  • @nos 抱歉只是另一个问题.. 可以将此示例分成 .h 和 .cpp 对.. 因为当我将代码分开时,他们实际上会声明无法解决排序问题。跨度>
【解决方案2】:

您必须同时修改函数 sortByName 的声明和定义才能接收到热带的 const 引用,如下所示:

bool sortByName(const tropical &t1, const tropical &t2); //Prototype (declaration)


bool sortByName(const tropical &t1, const tropical &t2)  //definition
{
    return t1.name < t2.name;
}

【讨论】:

    【解决方案3】:

    尝试将 sortByName 的 args 定义为 const。

    // Declaration
    bool sortByName(const tropical t1, const tropical t2);
    
    // Implementation
    bool sortByName(const tropical t1, const tropical t2)
    {
        return t1.name < t2.name;
    }
    

    【讨论】:

    • 也尝试更改声明:bool sortByName(consttropical t1, consttropical t2);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多