Strange vector problem

P

Peng

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);
}
 
M

Matthew Del Buono

Peng said:
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*)
 
L

Leor Zolman

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
 
D

David Rubin

Peng said:
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
 
P

Peng

Thank you. I think that should be the problem.

eng

Leor Zolman said:
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
 
D

David Rubin

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
 
L

Leor Zolman

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
 
L

Leor Zolman

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
 
M

Michiel Salters

David Rubin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top