string array ...

D

dog

I want to have a string array as private member, a then in constructor
set the values in array, something like this:

class obj{
string s[];
public:
obj(){
s = {"bla", "blaaa", "..."};
}
};

but, it wont compile ... please help
 
G

Guest

I want to have a string array as private member, a then in constructor
set the values in array, something like this:

class obj{
string s[];
public:
obj(){
s = {"bla", "blaaa", "..."};
}
};

but, it wont compile ... please help

You must specify the size of the arry, and you can not use that syntax
to initialise an array of strings.

class obj{
string s[3];
public:
obj(){
s[0] = "bla";
s[1] = "blaaa";
s[2] = "...";
}
};
 
C

Chris ( Val )

I want to have a string array as private member, a then in constructor
set the values in array, something like this:

class obj{
string s[];

A C++ array requires a size to be specified for the
number of elements you require.
public:
obj(){
s = {"bla", "blaaa", "..."};

[snip]

You cannot assign to a C++ array like that. You
may be able to assign a new std::string array to
a std::string* pointer, but I don't reccomend it.

I also do not reccomend what you're attempting to
do in the constructor with const string literals.

An appropriae data structure to use here might be a
std::vector<std::string>, which is initialised in the
constructor initialiser list.
 
J

Jim Langston

dog said:
I want to have a string array as private member, a then in constructor set
the values in array, something like this:

class obj{
string s[];
public:
obj(){
s = {"bla", "blaaa", "..."};
}
};

but, it wont compile ... please help

As other's stated the work around, I'll just mention that perhaps you should
use std::vector instead of a fixed array.

class obj
{
std::vector<std::string> s;
obj()
{
s.push_back("bla");
s.push_back("Blaaa");
s.push_back("...");
}
};
 

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,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top