standard template library: sort() compile error

This is a discussion on standard template library: sort() compile error within the C++ Programming forums, part of the General Programming Boards category; Hello, i get some error while compiling the following little stl demo. first the errors: g++ -o stldemo stldemo.cpp stldemo.cpp: ...

  1. #1
    hampel
    Guest

    standard template library: sort() compile error

    Hello,

    i get some error while compiling the following little stl demo.

    first the errors:

    g++ -o stldemo stldemo.cpp
    stldemo.cpp: In function `bool operator<(mark&, mark&)':
    stldemo.cpp:20: cannot convert `bool' to `const char*' for argument `1' to `int
    strcmp(const char*, const char*)'
    /usr/include/c++/3.2/bits/stl_algo.h: In function `const _Tp&
    std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = mark]':
    /usr/include/c++/3.2/bits/stl_algo.h:2118: instantiated from `void std::__introsort_loop(_RandomAccessIter, _RandomAccessIter, _Size) [with _RandomAccessIter = __gnu_cxx::__normal_iterator<mark*, std::vector<mark, std::allocator<mark> > >, _Size =
    int]'
    /usr/include/c++/3.2/bits/stl_algo.h:2178: instantiated from `void std::sort(_RandomAccessIter, _RandomAccessIter) [with _RandomAccessIter = __gnu_cxx::__normal_iterator<mark*, std::vector<mark, std::allocator<mark> > >]'
    stldemo.cpp:61: instantiated from here
    /usr/include/c++/3.2/bits/stl_algo.h:90: no match for `const mark& < const
    mark&' operator
    stldemo.cpp:19: candidates are: bool operator<(mark&, mark&)
    ...

    And here is the programm:

    #include <iostream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    struct mark
    {
    char name[50];
    unsigned short mk;
    };

    void putmk(mark &m)
    {
    cout << m.name << ": " << m.mk << "\n";
    }

    bool operator < (mark &m1, mark &m2) // Vergleichsoperator < für Sortierung
    {
    if (strcmp(m1.name < m2.name) < 0)
    return (true);

    return (false);
    }

    int main(void)
    {
    vector <mark> a(2);
    vector <mark> b(3);

    // Werte in Container übertragen
    strcpy(a[0].name, "Caesar");
    strcpy(a[1].name, "Berta");
    strcpy(a[2].name, "Anton");
    strcpy(a[3].name, "Emil");
    strcpy(a[4].name, "Dora");

    a[0].mk = 5;
    a[1].mk = 4;
    b[0].mk = 3;
    b[1].mk = 2;
    b[2].mk = 1;

    cout << "Kurs A: " << a.size() << " Teilnehmer. "
    << "Maximale T-Zahl: " << a.max_size() << "\n";

    cout << "Kurs B: " << b.size() << " Teilnehmer. "
    << "Maximale T-Zahl: " << b.max_size() << "\n";

    cout << "\nKurs A:\n";

    for_each(a.begin(), a.end(), putmk); // for_each: STL - Schleifenersatz

    cout << "\nKurs B:\n";

    for_each(b.begin(), b.end(), putmk);

    cout << "Teilnehmertausch";
    a.swap(b); // swap: Containerinhalte vertauschen

    sort(a.begin(), a.end()); // Sortieren für Vergleichsoperationen
    sort(b.begin(), b.end());

    cout << "\nKurs A neu:\n";
    for_each(a.begin(), a.end(), putmk);

    cout << "\nKurs B neu:\n";
    for_each(b.begin(), b.end(), putmk);

    cout << "Kurs B in umgekehrter Reihenfolge:\n";
    for_each(b.rbegin(), b.rend(), putmk); // rückwärtszählender Iterator (Zeiger auf Containerelement)
    }

    I need help, please.
    Why do i get these errors?
    Perhaps forgotten linking a library?

    greetings,
    hampel

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Ahh.. aren't STL errors so wonderfully cryptic!

    First thing, in your 'operator<', strcmp needs a comma ',' separating args, not a '<'. Also, might want to make mark1 and mark2 const references... STL can be quite picky about that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    hampel
    Guest

    Problem solved, thanks

    Yes, they are VERY cryptic:-(

    Many thanks, you gave me the solution!

    greetings
    hampel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 10:09 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21