I mean I can't even get my program to compile, and I don't quite understand these compiler errors. I've always had trouble with STL
Page 1 of 1
C++ Stl Question Any programmers around here?
#1
Posted 10 August 2007 - 09:21 AM
I'm having a hell of a time figuring out how to make the multiset class work with an object. Every single example I find uses int, nothing showing how it works with an object. I'm totally stumped
I mean I can't even get my program to compile, and I don't quite understand these compiler errors. I've always had trouble with STL
I mean I can't even get my program to compile, and I don't quite understand these compiler errors. I've always had trouble with STL
『4-17』子曰:「見賢思齊焉; 見不賢而内自省也。」 里仁 論語
#2
Posted 10 August 2007 - 10:06 AM
multiset<object> blah = new multiset()... something like that.
but this is the wrong forum for that kinda stuff... com'n, we're all about kpop right?
but this is the wrong forum for that kinda stuff... com'n, we're all about kpop right?
#3
Posted 10 August 2007 - 10:15 AM
what are you trying to do, and what's the error you're getting?
multisets are the same as sets, except that you can have duplicate entries.
btw...
hyori is mine!
multisets are the same as sets, except that you can have duplicate entries.
btw...
hyori is mine!
#4
Posted 10 August 2007 - 10:25 AM
I think I got the creation of the set down ok - I'm at the point where I need to iterate over the objects in the set.
int main(void)
{
typedef multiset<foo, foo_sort> FooSet;
FooSet foo_set;
for (int i = 0; i < 10; i++) {
foo obj;
obj.fubar = i;
foo_set.insert(obj);
}
foo bar;
list<foo>::iterator it;
for (it = foo_set.begin(); it != foo_set.end(); it++) {
bar = foo(*it);
cout << bar.fubar << endl;
}
return 0;
}
This doesn't compile (line numbers are off as I haven't included the class definition):
$ g++ -g -Wall test.cpp
test.cpp: In function `int main()':
test.cpp:94: error: no match for 'operator=' in 'it = (&foo_set)->std::multiset<_Key, _Compare, _Alloc>::begin [with _Key = foo, _Compare = foo_sort, _Alloc = std::allocator<foo>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_list.h:112: note: candidates are: std::_List_iterator<foo>& std::_List_iterator<foo>::operator=(const std::_List_iterator<foo>&)
test.cpp:94: error: no match for 'operator!=' in 'it != (&foo_set)->std::multiset<_Key, _Compare, _Alloc>::end [with _Key = foo, _Compare = foo_sort, _Alloc = std::allocator<foo>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_list.h:173: note: candidates are: bool std::_List_iterator<_Tp>::operator!=(const std::_List_iterator<_Tp>&) const [with _Tp = foo]
eh... say what? My 'foo' class does indeed have operator= and operator!= overloaded, so I'm not sure what the problem is. Surely they don't expect me to overload those operators on the multiset or list class?
int main(void)
{
typedef multiset<foo, foo_sort> FooSet;
FooSet foo_set;
for (int i = 0; i < 10; i++) {
foo obj;
obj.fubar = i;
foo_set.insert(obj);
}
foo bar;
list<foo>::iterator it;
for (it = foo_set.begin(); it != foo_set.end(); it++) {
bar = foo(*it);
cout << bar.fubar << endl;
}
return 0;
}
This doesn't compile (line numbers are off as I haven't included the class definition):
$ g++ -g -Wall test.cpp
test.cpp: In function `int main()':
test.cpp:94: error: no match for 'operator=' in 'it = (&foo_set)->std::multiset<_Key, _Compare, _Alloc>::begin [with _Key = foo, _Compare = foo_sort, _Alloc = std::allocator<foo>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_list.h:112: note: candidates are: std::_List_iterator<foo>& std::_List_iterator<foo>::operator=(const std::_List_iterator<foo>&)
test.cpp:94: error: no match for 'operator!=' in 'it != (&foo_set)->std::multiset<_Key, _Compare, _Alloc>::end [with _Key = foo, _Compare = foo_sort, _Alloc = std::allocator<foo>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_list.h:173: note: candidates are: bool std::_List_iterator<_Tp>::operator!=(const std::_List_iterator<_Tp>&) const [with _Tp = foo]
eh... say what? My 'foo' class does indeed have operator= and operator!= overloaded, so I'm not sure what the problem is. Surely they don't expect me to overload those operators on the multiset or list class?
『4-17』子曰:「見賢思齊焉; 見不賢而内自省也。」 里仁 論語
#5
Posted 10 August 2007 - 11:14 AM
Nevermind, I got it. 'FooSet::iterator it' instead of 'list<foo>::iterator it'
『4-17』子曰:「見賢思齊焉; 見不賢而内自省也。」 里仁 論語
#6
Posted 10 August 2007 - 12:36 PM
Now apparently I need a lesson in how STL's find function works... As best I can figure out, it uses the < operator? But just to be safe, I defined all the comparison operators for my object.
class phone_data_struct {
public:
char cid[18];
...
bool operator <(const phone_data_struct &rhs) const {
return (strncmp(cid, rhs.cid, sizeof(cid)) < 0 ? true : false);
}
bool operator <=(const phone_data_struct &rhs) const {
return (strncmp(cid, rhs.cid, sizeof(cid)) <= 0 ? true : false);
}
bool operator==(const phone_data_struct &rhs) const {
return (strncmp(cid, rhs.cid, sizeof(cid)) == 0 ? true : false);
}
// etc. for the rest of the comparison operators
};
class phone_sort {
public:
bool operator() (const phone_data_struct &a, const phone_data_struct &b) const {
return (strncmp(a.cid, b.cid, sizeof(a.cid)));
}
};
int main(void)
{
typedef multiset<phone_data_struct, phone_sort> PhoneSet;
PhoneSet phone_data_list;
// Skip code that populates the set
phone_data_struct dummy(cur_trig.cid, " ", ' ');
PhoneSet::iterator itp = phone_data_list.find(dummy);
This NEVER finds a match. I looked in gdb and saw that the cur_trig.cid did indeed match the cid in at least one of the nodes of phone_data_list, so it SHOULD be returning SOMETHING, but it doesn't. Does it use something other than the operators I defined for phone_data_struct? Does it attempt to determine equality based on ALL the member variables or some other criteria? I'm so confused
class phone_data_struct {
public:
char cid[18];
...
bool operator <(const phone_data_struct &rhs) const {
return (strncmp(cid, rhs.cid, sizeof(cid)) < 0 ? true : false);
}
bool operator <=(const phone_data_struct &rhs) const {
return (strncmp(cid, rhs.cid, sizeof(cid)) <= 0 ? true : false);
}
bool operator==(const phone_data_struct &rhs) const {
return (strncmp(cid, rhs.cid, sizeof(cid)) == 0 ? true : false);
}
// etc. for the rest of the comparison operators
};
class phone_sort {
public:
bool operator() (const phone_data_struct &a, const phone_data_struct &b) const {
return (strncmp(a.cid, b.cid, sizeof(a.cid)));
}
};
int main(void)
{
typedef multiset<phone_data_struct, phone_sort> PhoneSet;
PhoneSet phone_data_list;
// Skip code that populates the set
phone_data_struct dummy(cur_trig.cid, " ", ' ');
PhoneSet::iterator itp = phone_data_list.find(dummy);
This NEVER finds a match. I looked in gdb and saw that the cur_trig.cid did indeed match the cid in at least one of the nodes of phone_data_list, so it SHOULD be returning SOMETHING, but it doesn't. Does it use something other than the operators I defined for phone_data_struct? Does it attempt to determine equality based on ALL the member variables or some other criteria? I'm so confused
『4-17』子曰:「見賢思齊焉; 見不賢而内自省也。」 里仁 論語
Share this topic:
Page 1 of 1











