Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ compilation error using #include<algorithm>
    primarykey
    data
    text
    <p>I'm having compilation error whenever i try to compile the programme.</p> <p>when I try to remove the "sort" function in this programme everything is fine, problem start arising when I use the "sort" function. </p> <p>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. </p> <p>here's the code</p> <pre><code>#include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; #include &lt;iterator&gt; #include &lt;vector&gt; #include &lt;string&gt; using namespace std; class tropical { public: string name; int price; }; bool sortByName(tropical &amp;t1, tropical &amp;t2); // void displayfruits(vector&lt;tropical&gt; fruitlist) { cout &lt;&lt; "Name \t\tPrice" &lt;&lt; endl &lt;&lt; "==========\t=====" &lt;&lt; endl; for(int i=0;i&lt;10;i++) //displays all fruits' names and prices { cout &lt;&lt; fruitlist[i].name &lt;&lt; " \t" &lt;&lt; fruitlist[i].price &lt;&lt; endl; } cout &lt;&lt; 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&lt;tropical&gt; fruitlist; tropical fruit; vector&lt;tropical&gt;::iterator it; for(int i=0; i&lt;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 &amp;t1, tropical &amp;t2) { return t1.name &lt; t2.name; } </code></pre> <p>this is error i'm getting</p> <pre><code> 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&amp;, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;tropical*, std::vector&lt;tropical&gt; &gt;, _Tp = tropical, _Compare = bool (*)(tropical&amp;, tropical&amp;)]': 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&lt;tropical*, std::vector&lt;tropical&gt; &gt;, _Compare = bool (*)(tropical&amp;, tropical&amp;)]' 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&lt;tropical*, std::vector&lt;tropical&gt; &gt;, _Size = int, _Compare = bool (*)(tropical&amp;, tropical&amp;)]' 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&lt;tropical*, std::vector&lt;tropical&gt; &gt;, _Compare = bool (*)(tropical&amp;, tropical&amp;)]' ..\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&amp;' 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&amp;' from expression of type 'const tropical' </code></pre> <p>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?</p> <p><a href="http://www.daniweb.com/software-development/cpp/threads/242984/sorting-multiple-array-containers-with-related-elements#post1064721" rel="nofollow">http://www.daniweb.com/software-development/cpp/threads/242984/sorting-multiple-array-containers-with-related-elements#post1064721</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload