By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

Strange vector problem

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.