std::vector<user-defined class>
Author |
Message |
Steve Carrol #1 / 11
|
 std::vector<user-defined class>
Hi all, Using Visual C++ version 5, is it possible to declare a std::vector<> of a user-defined class? I just tried and was told that there is no acceptable conversion. I tried the same declaration with the Borland compiler v5.5 and had no problem. Am I doing something wrong? My declaration:- std::vector<ClientInfo> vClientList; Thanks in advance, ... Steve C.
|
Sat, 13 Dec 2003 21:35:39 GMT |
|
 |
Igor Tandetni #2 / 11
|
 std::vector<user-defined class>
How about a small compilable fragment that reproduces the problem? -- With best wishes, Igor Tandetnik
Quote: > Hi all, > Using Visual C++ version 5, is it possible to declare a > std::vector<> of a user-defined class? I just tried and was told that there > is no acceptable conversion. I tried the same declaration with the Borland > compiler v5.5 and had no problem. Am I doing something wrong? > My declaration:- > std::vector<ClientInfo> vClientList; > Thanks in advance, > ... Steve C.
|
Sat, 13 Dec 2003 22:17:54 GMT |
|
 |
Das #3 / 11
|
 std::vector<user-defined class>
Im not an expert by you could try : typedef std::vector<ClientInfo> vClientList; If that doesnt work it could be you need to create a copy constructor or assignment operator. Das -- Software Engineer Telspec Europe Ltd
Quote: > Hi all, > Using Visual C++ version 5, is it possible to declare a > std::vector<> of a user-defined class? I just tried and was told that there > is no acceptable conversion. I tried the same declaration with the Borland > compiler v5.5 and had no problem. Am I doing something wrong? > My declaration:- > std::vector<ClientInfo> vClientList; > Thanks in advance, > ... Steve C.
|
Sat, 13 Dec 2003 22:26:10 GMT |
|
 |
Craig Power #4 / 11
|
 std::vector<user-defined class>
Quote:
> Using Visual C++ version 5, is it possible to declare a > std::vector<> of a user-defined class?
Yes. Quote: > I just tried and was told that there > is no acceptable conversion. I tried the same declaration with the Borland > compiler v5.5 and had no problem. Am I doing something wrong? > My declaration:- > std::vector<ClientInfo> vClientList;
There's nothing wrong with the declaration. What's the error message, and what's the line of code to which it refers?
|
Sat, 13 Dec 2003 22:22:56 GMT |
|
 |
Steve Carrol #5 / 11
|
 std::vector<user-defined class>
Hi Igor, I've pasted a copy of my "ClientInfo" class declaration below. (There's not much to go wrong here. If I include it, along with <vector>, I should then be able to globally declare the following, (shouldn't I?):- std::vector<ClientInfo> vClientList; // clientinfo.h // Declaration of 'ClientInfo' class. #ifndef CLIENT_INFO_H #define CLIENT_INFO_H const int CI_FIELD_NUM = 8; // Number of client data fields. const int CI_FIELD_CHAR = 15; // Number of char in field names. const int CI_FIELD_SIZE = 256; // Number of char per field. enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; class ClientInfo { public: char szInfArray[CI_FIELD_NUM][CI_FIELD_SIZE]; // Array of client data fields. char szFieldNameArray[CI_FIELD_NUM][CI_FIELD_CHAR]; // Array of field names. #define FNA szFieldNameArray // I got lazy. ClientInfo() { strcpy(FNA[0], "Surname"); // Fill array of field names. strcpy(FNA[1], "First Name"); strcpy(FNA[2], "Street Address"); strcpy(FNA[3], "Town"); strcpy(FNA[4], "Postcode"); strcpy(FNA[5], "Area Code"); strcpy(FNA[6], "Phone"); strcpy(FNA[7], "E-mail"); } #undef FNA ~ClientInfo(){} Quote: };
#endif // CLIENT_INFO_H ... Steve C.
Quote: > How about a small compilable fragment that reproduces the problem? > With best wishes, > Igor Tandetnik
|
Sat, 13 Dec 2003 22:44:06 GMT |
|
 |
Steve Carrol #6 / 11
|
 std::vector<user-defined class>
Hi Craig, I've pasted the errors below. They don't relate to my files, but to the original include files:- C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class ClientInfo' (or there is no acceptable conversion) C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class ClientInfo' (or there is no acceptable conversion) I'm trying this in a dialog app, in the main .cpp file. (My "ClientInfo.h" file is pasted into my reply to Igor.) ... Steve C.
Quote: > There's nothing wrong with the declaration. What's the error message, > and what's the line of code to which it refers?
|
Sat, 13 Dec 2003 22:53:05 GMT |
|
 |
Steve Carrol #7 / 11
|
 std::vector<user-defined class>
Das, I tried "typedef", but then vClientList isn't recognised if I test it with the following code:- ClientInfo ci; for(int n=0;n<8;n++) ci.szInfArray[n][0] = '\0'; strcpy(ci.szInfArray[0], "Steve"); vClientList.push_back(ci); The compiler says:- " 'vClientList' : illegal use of this type as an expression ", among other things. I'll have to read up on creating a "copy constructor" or assignment operator. ... Steve C.
Quote: > Im not an expert by you could try : > typedef std::vector<ClientInfo> vClientList; > If that doesnt work it could be you need to create a copy constructor or > assignment operator. > Das > Software Engineer > Telspec Europe Ltd
|
Sat, 13 Dec 2003 23:11:38 GMT |
|
 |
Igor Tandetni #8 / 11
|
 std::vector<user-defined class>
The following program compiles and runs on my MSVC 6 SP 5: #include <vector> #include <iostream> const int CI_FIELD_NUM = 8; // Number of client data fields. const int CI_FIELD_CHAR = 15; // Number of char in field names. const int CI_FIELD_SIZE = 256; // Number of char per field. enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; class ClientInfo { // The exact copy of ClientInfo definition skipped Quote: };
std::vector<ClientInfo> vClientList; int main() { vClientList.push_back(ClientInfo()); std::cout << vClientList.size() << std::endl; return 0; Quote: }
Do you try things like std::sort, binary_search - something that requires that the elements be comparable? Error message suggests you do. That's the reason I insist on a small compilable fragment - I can't read minds, unfortunately, and you stubbornly refuse to show the code that actually causes the problem. -- With best wishes, Igor Tandetnik
Quote: > Hi Igor, > I've pasted a copy of my "ClientInfo" class declaration > below. (There's not much to go wrong here. If I include it, along with > <vector>, I should then be able to globally declare the following, > (shouldn't I?):- > std::vector<ClientInfo> vClientList; > // clientinfo.h > // Declaration of 'ClientInfo' class. > #ifndef CLIENT_INFO_H > #define CLIENT_INFO_H > const int CI_FIELD_NUM = 8; // Number of client data fields. > const int CI_FIELD_CHAR = 15; // Number of char in field names. > const int CI_FIELD_SIZE = 256; // Number of char per field. > enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, > POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; > class ClientInfo > { > public: > char szInfArray[CI_FIELD_NUM][CI_FIELD_SIZE]; // Array of client data > fields. > char szFieldNameArray[CI_FIELD_NUM][CI_FIELD_CHAR]; // Array of field > names. > #define FNA szFieldNameArray // I got lazy. > ClientInfo() > { > strcpy(FNA[0], "Surname"); // Fill array of field names. > strcpy(FNA[1], "First Name"); > strcpy(FNA[2], "Street Address"); > strcpy(FNA[3], "Town"); > strcpy(FNA[4], "Postcode"); > strcpy(FNA[5], "Area Code"); > strcpy(FNA[6], "Phone"); > strcpy(FNA[7], "E-mail"); > } > #undef FNA > ~ClientInfo(){} > }; > #endif // CLIENT_INFO_H > ... Steve C.
> > How about a small compilable fragment that reproduces the problem? > > With best wishes, > > Igor Tandetnik
|
Sat, 13 Dec 2003 23:15:02 GMT |
|
 |
Steve Carrol #9 / 11
|
 std::vector<user-defined class>
Igor, I'm not refusing to show the code. I don't even get far enough to write any, beyond what I've showed you. (Sorry, I do declare an instance of my "ClientInfo" class, ( ClientInfo ci; ), without incident. I'm doing this in a dialog app. and don't get past the includes and the initial declaration that worked for you, ( std::vector<ClientInfo> vClientList; ). After the includes and the declaration above, if I try to compile I get these messages:- C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class ClientInfo' (or there is no acceptable conversion) C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class ClientInfo' (or there is no acceptable conversion) ... Steve C.
Quote: > The following program compiles and runs on my MSVC 6 SP 5: > #include <vector> > #include <iostream> > const int CI_FIELD_NUM = 8; // Number of client data fields. > const int CI_FIELD_CHAR = 15; // Number of char in field names. > const int CI_FIELD_SIZE = 256; // Number of char per field. > enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, > POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; > class ClientInfo > { > // The exact copy of ClientInfo definition skipped > }; > std::vector<ClientInfo> vClientList; > int main() > { > vClientList.push_back(ClientInfo()); > std::cout << vClientList.size() << std::endl; > return 0; > } > Do you try things like std::sort, binary_search - something that requires > that the elements be comparable? Error message suggests you do. That's the > reason I insist on a small compilable fragment - I can't read minds, > unfortunately, and you stubbornly refuse to show the code that actually > causes the problem. > -- > With best wishes, > Igor Tandetnik
> > Hi Igor, > > I've pasted a copy of my "ClientInfo" class declaration > > below. (There's not much to go wrong here. If I include it, along with > > <vector>, I should then be able to globally declare the following, > > (shouldn't I?):- > > std::vector<ClientInfo> vClientList; > > // clientinfo.h > > // Declaration of 'ClientInfo' class. > > #ifndef CLIENT_INFO_H > > #define CLIENT_INFO_H > > const int CI_FIELD_NUM = 8; // Number of client data fields. > > const int CI_FIELD_CHAR = 15; // Number of char in field names. > > const int CI_FIELD_SIZE = 256; // Number of char per field. > > enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, > > POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; > > class ClientInfo > > { > > public: > > char szInfArray[CI_FIELD_NUM][CI_FIELD_SIZE]; // Array of client data > > fields. > > char szFieldNameArray[CI_FIELD_NUM][CI_FIELD_CHAR]; // Array of field > > names. > > #define FNA szFieldNameArray // I got lazy. > > ClientInfo() > > { > > strcpy(FNA[0], "Surname"); // Fill array of field names. > > strcpy(FNA[1], "First Name"); > > strcpy(FNA[2], "Street Address"); > > strcpy(FNA[3], "Town"); > > strcpy(FNA[4], "Postcode"); > > strcpy(FNA[5], "Area Code"); > > strcpy(FNA[6], "Phone"); > > strcpy(FNA[7], "E-mail"); > > } > > #undef FNA > > ~ClientInfo(){} > > }; > > #endif // CLIENT_INFO_H > > ... Steve C.
> > > How about a small compilable fragment that reproduces the problem? > > > With best wishes, > > > Igor Tandetnik
|
Sat, 13 Dec 2003 23:30:39 GMT |
|
 |
Igor Tandetni #10 / 11
|
 std::vector<user-defined class>
Unfortunately, you have VC5, and I have VC6, so our system headers are different and what's in my <xutility> on line 45 is not in yours (at least there's nothing there in mine that could cause this error). Does my simple program compile for you? Is what you show a complete error message text? Isn't there something like "while compiling line xxx in file yyy"? What's in your xutility header between lines 40 and 55 (just a bit of context)? -- With best wishes, Igor Tandetnik
Quote: > Igor, I'm not refusing to show the code. I don't even get far enough to > write any, beyond what I've showed you. (Sorry, I do declare an instance of > my "ClientInfo" class, ( ClientInfo ci; ), without incident. I'm doing this > in a dialog app. and don't get past the includes and the initial declaration > that worked for you, ( std::vector<ClientInfo> vClientList; ). After the > includes and the declaration above, if I try to compile I get these > messages:- > C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2678: binary '<' > : no operator defined which takes a left-hand operand of type 'const class > ClientInfo' (or there is no acceptable conversion) > C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2678: binary '<' > : no operator defined which takes a left-hand operand of type 'const class > ClientInfo' (or there is no acceptable conversion) > ... Steve C.
> > The following program compiles and runs on my MSVC 6 SP 5: > > #include <vector> > > #include <iostream> > > const int CI_FIELD_NUM = 8; // Number of client data fields. > > const int CI_FIELD_CHAR = 15; // Number of char in field names. > > const int CI_FIELD_SIZE = 256; // Number of char per field. > > enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, > > POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; > > class ClientInfo > > { > > // The exact copy of ClientInfo definition skipped > > }; > > std::vector<ClientInfo> vClientList; > > int main() > > { > > vClientList.push_back(ClientInfo()); > > std::cout << vClientList.size() << std::endl; > > return 0; > > } > > Do you try things like std::sort, binary_search - something that requires > > that the elements be comparable? Error message suggests you do. That's the > > reason I insist on a small compilable fragment - I can't read minds, > > unfortunately, and you stubbornly refuse to show the code that actually > > causes the problem. > > -- > > With best wishes, > > Igor Tandetnik
> > > Hi Igor, > > > I've pasted a copy of my "ClientInfo" class > declaration > > > below. (There's not much to go wrong here. If I include it, along with > > > <vector>, I should then be able to globally declare the following, > > > (shouldn't I?):- > > > std::vector<ClientInfo> vClientList; > > > // clientinfo.h > > > // Declaration of 'ClientInfo' class. > > > #ifndef CLIENT_INFO_H > > > #define CLIENT_INFO_H > > > const int CI_FIELD_NUM = 8; // Number of client data fields. > > > const int CI_FIELD_CHAR = 15; // Number of char in field names. > > > const int CI_FIELD_SIZE = 256; // Number of char per field. > > > enum{SURNAME, FIRSTNAME, STREETADDRESS, TOWN, > > > POSTCODE, AREACODE, PHONE, EMAIL}CI_FIELD; > > > class ClientInfo > > > { > > > public: > > > char szInfArray[CI_FIELD_NUM][CI_FIELD_SIZE]; // Array of client > data > > > fields. > > > char szFieldNameArray[CI_FIELD_NUM][CI_FIELD_CHAR]; // Array of > field > > > names. > > > #define FNA szFieldNameArray // I got lazy. > > > ClientInfo() > > > { > > > strcpy(FNA[0], "Surname"); // Fill array of field names. > > > strcpy(FNA[1], "First Name"); > > > strcpy(FNA[2], "Street Address"); > > > strcpy(FNA[3], "Town"); > > > strcpy(FNA[4], "Postcode"); > > > strcpy(FNA[5], "Area Code"); > > > strcpy(FNA[6], "Phone"); > > > strcpy(FNA[7], "E-mail"); > > > } > > > #undef FNA > > > ~ClientInfo(){} > > > }; > > > #endif // CLIENT_INFO_H > > > ... Steve C.
> > > > How about a small compilable fragment that reproduces the problem? > > > > With best wishes, > > > > Igor Tandetnik
|
Sat, 13 Dec 2003 23:53:16 GMT |
|
 |
P.J. Plauge #11 / 11
|
 std::vector<user-defined class>
Quote:
> C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2678: binary '<' > : no operator defined which takes a left-hand operand of type 'const class > ClientInfo' (or there is no acceptable conversion)
It's a V5 bug. You have to give it a definition of operator< for your class even if you never call it. Fixed in V6. P.J. Plauger Dinkumware, Ltd. http://www.dinkumware.com
|
Sun, 14 Dec 2003 03:58:18 GMT |
|
|
|