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

cannot recognize vector as a vector, please help

P: n/a
Hi all,
Please help see why this snippet does not compile. Thanks a lot.
John
#include <string>
#include <vector>
namespace PatternMsg
{
std::vector<std::string> msg(17);
msg[0] = "First string";
}

4 errors are in the last line:

error C2466: cannot allocate an array of constant size 0
error C2501: 'msg' : missing storage-class or type specifiers
error C2040: 'msg' : 'int []' differs in levels of indirection from
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::al
locator<char> >,class std::allocator<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > > >'
error C2440: 'initializing' : cannot convert from 'char []' to 'int []'
There is no context in which this conversion is possible

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
John Guo wrote:
Hi all,
Please help see why this snippet does not compile. Thanks a lot.
John
#include <string>
#include <vector>
namespace PatternMsg
{
std::vector<std::string> msg(17);
msg[0] = "First string";
}


The error message is deeply unhelpful, but you have to put your code
inside a function :) Only very limited initalisation is allowed outside
functions.

#include <string>
#include <vector>

namespace PatternMsg
{
std::vector<std::string> msg(17);
void initalise_vector()
{ msg[0] = "First string"; }
}

is fine (there are numerous ways you can accomplish the same thing, but
the most imporant thing is that you must declare the elements of the
vector in a function).

Chris
Jul 23 '05 #2

P: n/a
Thanks Chris.

The only thing I want to achieve is to have an array of constant
strings predefined. It seems to me that if I use the vector of string
approach, I have to initialize it by calling a function. This is
awkward. I am originally using char[][], and init it in the namespace.

Could someone suggest a more elegant approach to do the same thing?

Thanks,

Jul 23 '05 #3

P: n/a
John Guo schrieb:
Thanks Chris.

The only thing I want to achieve is to have an array of constant
strings predefined. It seems to me that if I use the vector of string
approach, I have to initialize it by calling a function. This is
awkward. I am originally using char[][], and init it in the namespace.

Could someone suggest a more elegant approach to do the same thing?


You could use an accessor function:

typedef std::vector< std::string > PatternsMsgs;

const PatternMsgs& patternMsgs()
{
static PatternMsgs messages;
static bool initialised = false;
if ( !initialised )
{
// fill the vector
initialised = true;
}
return messages;
}

that is if the messages are constant. Make it return a non-const
reference otherwise.

Cheers,
Malte
Jul 23 '05 #4

P: n/a
John Guo wrote:
Thanks Chris.

The only thing I want to achieve is to have an array of constant
strings predefined. It seems to me that if I use the vector of string
approach, I have to initialize it by calling a function. This is
awkward. I am originally using char[][], and init it in the namespace.

Could someone suggest a more elegant approach to do the same thing?

Thanks,


There is no method to have an STL container
initialized with constant data the same way
that an array is.

One can declare an initialized array:
const int const_array[5] = {0, 1, 2, 3, 4};
But a vector can't:
const std::vector<int> const_vector = {0, 1, 2, 3, 4};

I would really like to have an std::map declared
in this manner, but there is no method. Many
people suggested having the data in an array,
then loading the container from the array. Hmmm,
duplicate memory and why not just use the array?
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #5

P: n/a
John Guo wrote:
Hi all,
Please help see why this snippet does not compile. Thanks a lot.
John
#include <string>
#include <vector>
namespace PatternMsg
{
std::vector<std::string> msg(17);
msg[0] = "First string";
}

4 errors are in the last line:


Would something like this work:

//---
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;

typedef std::vector<std::string> string_vector;

string_vector::value_type msg[] = {"one", "two", "three", "four"};
string_vector data(msg, msg + sizeof(msg)/sizeof(*msg));

int main(int, char**) {
// just an example to print out all of the elements
for (string_vector::iterator it = data.begin(); it != data.end(); *it++) {
cout << *it << endl;
}
return 0;
}
//---

compiled fine in strict mode.

--
Peter MacMillan
e-mail/msn: pe***@writeopen.com
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
Jul 23 '05 #6

P: n/a
Thomas Matthews wrote:
John Guo wrote:
One can declare an initialized array:
const int const_array[5] = {0, 1, 2, 3, 4};
But a vector can't:
const std::vector<int> const_vector = {0, 1, 2, 3, 4};


const int const_array[5] = { 0, 1, 2, 3, 4 };
const std::vector<int> const_vector(const_array, const_array+5);
Jul 23 '05 #7

P: n/a
Malte Starostik wrote:
John Guo schrieb:
Thanks Chris.

The only thing I want to achieve is to have an array of constant
strings predefined. It seems to me that if I use the vector of string
approach, I have to initialize it by calling a function. This is
awkward. I am originally using char[][], and init it in the namespace.

Could someone suggest a more elegant approach to do the same thing?

You could use an accessor function:

typedef std::vector< std::string > PatternsMsgs;

const PatternMsgs& patternMsgs()
{
static PatternMsgs messages;
static bool initialised = false;
if ( !initialised )
{
// fill the vector
initialised = true;
}
return messages;
}

that is if the messages are constant. Make it return a non-const
reference otherwise.

Cheers,
Malte


Another variation I like to use myself is to enclose te data item in a
structure with a constructor:

typedef std::vector<std::string> PatternMsgs;

struct PatternMsgsInit {
PatternMsgs data;

PatternMsgsInit()
{
data.add("blah");
...
}
};

PatternMsgsInit msgs;

Doing this results in creating the data once and not needing to check it each
time you try to access it.
Jul 23 '05 #8

P: n/a
red floyd wrote:
Thomas Matthews wrote:
John Guo wrote:
One can declare an initialized array:
const int const_array[5] = {0, 1, 2, 3, 4};
But a vector can't:
const std::vector<int> const_vector = {0, 1, 2, 3, 4};

const int const_array[5] = { 0, 1, 2, 3, 4 };
const std::vector<int> const_vector(const_array, const_array+5);


Again, my point. You have two sets of the data.
Can a vector be initialized without the array method,
thus removing the need for duplicate data?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #9

P: n/a
* Thomas Matthews:
red floyd wrote:
Thomas Matthews wrote:
John Guo wrote:
One can declare an initialized array:
const int const_array[5] = {0, 1, 2, 3, 4};
But a vector can't:
const std::vector<int> const_vector = {0, 1, 2, 3, 4};

const int const_array[5] = { 0, 1, 2, 3, 4 };
const std::vector<int> const_vector(const_array, const_array+5);


Again, my point. You have two sets of the data.
Can a vector be initialized without the array method,
thus removing the need for duplicate data?


No: the data must be present in some form outside the resulting vector.

What you need for specifying the data as a raw array and have that raw array
used directly instead of copied is something that presents the read-only
portion of a vector interface but just holds pointers to constant data;
however, while that could be very useful for code made for that interface, and
perhaps also for templated code, it would not work where an actual std::vector
is required other than by providing a copy conversion to std::vector.

I've been thinking lately that providing a read-only interface for every
class could conceivably be a good alternative to ordinary const'ness, and it
would have allowed what you seem to wish (although, you don't describe what
functionality you really want: if it's all of vector then the requirements
are impossible to implement, AFAICS).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.