469,924 Members | 1,416 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

Strange vector problem

Hi, I have encountered a simple but strange problem recently, with the STL
vector.
When I tried to compile a code like this, the compiler flags an error:

Error 212: "small.cc", line 200 # Argument type 'char *' does not match
expected parameter type 'const char (&)[9]'.
names.push_back(str);
^^^
can any one tell me why? Thanks.

Peng

PS the code

typedef char namestring [9]
typedef vector<namestring> NameArray; // name structure
NameArray names; // a global array

void lookup (namestring str)
{
names.push_back(str);
}
Jul 22 '05 #1
8 1681

"Peng" <py***@delete-this.cam.ac.uk> wrote in message
news:c7**********@pegasus.csx.cam.ac.uk...
Hi, I have encountered a simple but strange problem recently, with the STL
vector.
When I tried to compile a code like this, the compiler flags an error:

Error 212: "small.cc", line 200 # Argument type 'char *' does not match
expected parameter type 'const char (&)[9]'.
names.push_back(str);
^^^
can any one tell me why? Thanks.

Peng

PS the code

typedef char namestring [9]
typedef vector<namestring> NameArray; // name structure
NameArray names; // a global array

void lookup (namestring str)
{
names.push_back(str);
}


Just a thought, but try dereferencing it
That is, instead of str, do *str
(assuming str is a char*)
Jul 22 '05 #2
On Wed, 12 May 2004 03:04:38 +0100, "Peng" <py***@delete-this.cam.ac.uk>
wrote:
Hi, I have encountered a simple but strange problem recently, with the STL
vector.
When I tried to compile a code like this, the compiler flags an error:

Error 212: "small.cc", line 200 # Argument type 'char *' does not match
expected parameter type 'const char (&)[9]'.
names.push_back(str);
^^^
can any one tell me why? Thanks.

Peng

PS the code

typedef char namestring [9]
typedef vector<namestring> NameArray; // name structure
NameArray names; // a global array

void lookup (namestring str)
{
names.push_back(str);
}


You can't use a native array as a value_type in an STL container, because
arrays are not copy-assignable (one of the requirements of objects that can
be stored in STL containers). I recommend storing std::string objects in
the vector instead.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
Thank you. I think that should be the problem.

eng

"Leor Zolman" <le**@bdsoft.com> ????
news:6q********************************@4ax.com...
On Wed, 12 May 2004 03:04:38 +0100, "Peng" <py***@delete-this.cam.ac.uk>
wrote:
Hi, I have encountered a simple but strange problem recently, with the STLvector.
When I tried to compile a code like this, the compiler flags an error:

Error 212: "small.cc", line 200 # Argument type 'char *' does not match
expected parameter type 'const char (&)[9]'.
names.push_back(str);
^^^
can any one tell me why? Thanks.

Peng

PS the code

typedef char namestring [9]
typedef vector<namestring> NameArray; // name structure
NameArray names; // a global array

void lookup (namestring str)
{
names.push_back(str);
}

You can't use a native array as a value_type in an STL container, because
arrays are not copy-assignable (one of the requirements of objects that

can be stored in STL containers). I recommend storing std::string objects in
the vector instead.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html

Jul 22 '05 #4
Peng wrote:
Hi, I have encountered a simple but strange problem recently, with the STL
vector.
When I tried to compile a code like this, the compiler flags an error:

Error 212: "small.cc", line 200 # Argument type 'char *' does not match
expected parameter type 'const char (&)[9]'.
names.push_back(str);
^^^
can any one tell me why? Thanks.

Peng

PS the code

typedef char namestring [9]
typedef vector<namestring> NameArray; // name structure
NameArray names; // a global array

void lookup (namestring str)
{
names.push_back(str);
}


Not so strange. What does your compiler say about this equivalent program?

#include <vector>
int main() {
std::vector<char[9]> v(1);
return 0;
}

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 22 '05 #5
On Wed, 12 May 2004 03:04:38 +0100, "Peng" <py***@delete-this.cam.ac.uk>
wrote:
Hi, I have encountered a simple but strange problem recently, with the STL
vector.
When I tried to compile a code like this, the compiler flags an error:

Error 212: "small.cc", line 200 # Argument type 'char *' does not match
expected parameter type 'const char (&)[9]'.
names.push_back(str);
^^^
can any one tell me why? Thanks.

Peng

PS the code

typedef char namestring [9]
typedef vector<namestring> NameArray; // name structure
NameArray names; // a global array

void lookup (namestring str)
{
names.push_back(str);
}


You can't use a native array as a value_type in an STL container, because
arrays are neither copy-constructible nor assignable (two requirements of
objects that can be stored in STL containers). I recommend storing
std::string objects in the vector instead.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #6
David Rubin wrote:

[snip]
Not so strange. What does your compiler say about this equivalent program? ^^^^^^^^^^
Actually, I don't think they are equivalent, but they have the same flavor.

#include <vector>
int main() {
std::vector<char[9]> v(1);
return 0;
}

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 22 '05 #7
On Wed, 12 May 2004 03:03:21 GMT, Leor Zolman <le**@bdsoft.com> wrote:


You can't use a native array as a value_type in an STL container, because
arrays are neither copy-constructible nor assignable (two requirements of
objects that can be stored in STL containers). I recommend storing
std::string objects in the vector instead.
-leor


Sorry for the double-post... I've had news server problems tonight. On
top of that, a dialog box Agent put up led me to believe I had the
option of actually "replacing" a previous post with an updated
version. I thought to myself, "Gee, this is great! If I post something
with a typo, I can just update it!" and proceeded to attempt that for
this post. Clearly it doesn't do that...probably just as well. I guess
it would be pretty chaotic if anyone could dynamically correct their
posts so that responses to the original version would no longer make
sense...
-leor

Jul 22 '05 #8
David Rubin <fu******@warpmail.net> wrote in message news:<Fi*********************@twister.nyc.rr.com>. ..
What does your compiler say about this ... program?

#include <vector>
int main() {
std::vector<char[9]> v(1);
return 0;
}


Invalid argument to std::vector - cannot copy char[9].

Regards,
Michiel Salters.
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil | last post: by
3 posts views Thread by Al Newton | last post: by
7 posts views Thread by cppaddict | last post: by
7 posts views Thread by Hamish | last post: by
2 posts views Thread by Leszek Taratuta | last post: by
3 posts views Thread by Mike Durham | last post: by
1 post views Thread by zl2k | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.