Dereferencing a list Structure

Discussion in 'C++' started by Mike Copeland, Nov 29, 2008.

  1. In the following code, I get a C2679 error that I don't know how to
    handle. The error says I don't have a "binary = operator", but I don't
    know what that means, nor how to correct it.
    I guess the cause of the problem lies with the string within the
    struct, but I need the comparison operator for the sort, don't I?
    Please advise. TIA

    #include <string>
    #include <iostream>
    #include <list>
    #include <algorithm>
    using namespace std;

    struct TIMESTRUCT
    {
    string time_bib;
    bool operator <(const TIMESTRUCT &rhs) const
    {
    return time_bib < rhs.time_bib;
    }
    } workTime;
    typedef list<TIMESTRUCT> TIMEVEC;
    TIMEVEC timeVect;
    list<TIMESTRUCT>::iterator timeIter;
    int main(int argc, char *argv[])
    {
    string wsT;

    workTime.time_bib = "Sam", timeVect.push_back(workTime);
    workTime.time_bib = "Libby", timeVect.push_back(workTime);
    workTime.time_bib = "Bob", timeVect.push_back(workTime);
    workTime.time_bib = "Carol", timeVect.push_back(workTime);
    workTime.time_bib = "Ted", timeVect.push_back(workTime);
    workTime.time_bib = "Alice", timeVect.push_back(workTime);
    sort(timeVect.begin(), timeVect.end());
    for(timeIter = timeVect.begin(); timeIter != timeVect.end();
    timeIter++)
    {
    workTime = timeIter->time_bib; <- C2679 error
    }
    return;
    }
     
    Mike Copeland, Nov 29, 2008
    #1
    1. Advertising

  2. Mike Copeland

    Kai-Uwe Bux Guest

    Mike Copeland wrote:

    > In the following code, I get a C2679 error that I don't know how to
    > handle. The error says I don't have a "binary = operator", but I don't
    > know what that means, nor how to correct it.
    > I guess the cause of the problem lies with the string within the
    > struct, but I need the comparison operator for the sort, don't I?


    Well, either a comparison operator, or a specialization of std::less<>, or
    you could pass a comparison predicate to sort.

    BTW, where is a sort()?

    > Please advise. TIA
    >
    > #include <string>
    > #include <iostream>
    > #include <list>
    > #include <algorithm>
    > using namespace std;
    >
    > struct TIMESTRUCT
    > {
    > string time_bib;
    > bool operator <(const TIMESTRUCT &rhs) const
    > {
    > return time_bib < rhs.time_bib;
    > }
    > } workTime;
    > typedef list<TIMESTRUCT> TIMEVEC;
    > TIMEVEC timeVect;
    > list<TIMESTRUCT>::iterator timeIter;
    > int main(int argc, char *argv[])
    > {
    > string wsT;
    >
    > workTime.time_bib = "Sam", timeVect.push_back(workTime);
    > workTime.time_bib = "Libby", timeVect.push_back(workTime);
    > workTime.time_bib = "Bob", timeVect.push_back(workTime);
    > workTime.time_bib = "Carol", timeVect.push_back(workTime);
    > workTime.time_bib = "Ted", timeVect.push_back(workTime);
    > workTime.time_bib = "Alice", timeVect.push_back(workTime);
    > sort(timeVect.begin(), timeVect.end());
    > for(timeIter = timeVect.begin(); timeIter != timeVect.end();
    > timeIter++)
    > {
    > workTime = timeIter->time_bib; <- C2679 error


    timeIter->time_bib is of type std::string

    workTime is of type TIMESTRUCT

    There is no assignment operator that would allow you to assign a string to a
    TIMESTRUCT.

    Did you mean:

    workTime.time_bib = timeIter->time_bib;




    > }
    > return;
    > }


    PS.: It is usually good to reserve all upper case names for macros (and some
    people include global constants). For typenames, all upper case identifiers
    are probably best avoided.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 29, 2008
    #2
    1. Advertising

  3. Mike Copeland

    Rolf Magnus Guest

    Mike Copeland wrote:

    > In the following code, I get a C2679 error that I don't know how to
    > handle. The error says I don't have a "binary = operator", but I don't
    > know what that means, nor how to correct it.


    There is no such thing as a binary = operator.

    > I guess the cause of the problem lies with the string within the
    > struct, but I need the comparison operator for the sort, don't I?


    Yes. The comparison operator has nothing to do with it. Didn't your compiler
    tell you which operator= it wants? It doesn't know how to assign a string to
    a TIMESTRUCT, because you didn't write such an operator.
    I'd say that instead of this:

    > workTime = timeIter->time_bib; <- C2679 error


    You actually want:

    workTime = *timeIter;
     
    Rolf Magnus, Nov 29, 2008
    #3
  4. Mike Copeland

    Rolf Magnus Guest

    Mike Copeland wrote:

    > In the following code, I get a C2679 error that I don't know how to
    > handle. The error says I don't have a "binary = operator", but I don't
    > know what that means, nor how to correct it.
    > I guess the cause of the problem lies with the string within the
    > struct, but I need the comparison operator for the sort, don't I?


    Yes. The comparison operator has nothing to do with it. Didn't your compiler
    tell you which operator= it wants? It doesn't know how to assign a string to
    a TIMESTRUCT, because you didn't write such an operator.
    I'd say that instead of this:

    > workTime = timeIter->time_bib; <- C2679 error


    You actually want:

    workTime = *timeIter;
     
    Rolf Magnus, Nov 29, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Zimmermann
    Replies:
    2
    Views:
    491
    Rob Williscroft
    Apr 14, 2004
  2. Balog Pal
    Replies:
    0
    Views:
    560
    Balog Pal
    Dec 1, 2009
  3. Mike Copeland

    list Iterator Dereferencing Problem

    Mike Copeland, Sep 10, 2010, in forum: C++
    Replies:
    2
    Views:
    318
    Mike Copeland
    Sep 12, 2010
  4. Mike Copeland

    list Iterator Dereferencing Problem

    Mike Copeland, Sep 10, 2010, in forum: C++
    Replies:
    4
    Views:
    761
    Joshua Maurice
    Sep 12, 2010
  5. Replies:
    2
    Views:
    91
Loading...

Share This Page