Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dereferencing a list Structure

Reply
Thread Tools

Dereferencing a list Structure

 
 
Mike Copeland
Guest
Posts: n/a
 
      11-29-2008
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;
}

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-29-2008
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-29-2008
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;

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-29-2008
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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
list Iterator Dereferencing Problem Mike Copeland C++ 4 09-12-2010 06:41 AM
list Iterator Dereferencing Problem Mike Copeland C++ 2 09-12-2010 04:42 AM
Re: Dereferencing this in a constructor initialisation list Balog Pal C++ 0 12-01-2009 12:02 PM
Why doesen't dereferencing work in list context but referencing does? sln@netherlands.com Perl Misc 2 06-03-2009 02:49 AM
Dereferencing this in constructors initialiser list Martin Zimmermann C++ 2 04-14-2004 02:07 PM



Advertisments