473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

string array ...

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
Oct 4 '07 #1
4 2863
On 2007-10-04 11:58, dog wrote:
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] = "...";
}
};

--
Erik Wikström
Oct 4 '07 #2
On Oct 4, 7:58 pm, dog <d...@dog.comwrote:
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.

--
Chris Val

Oct 4 '07 #3
dog
Thank you very much
Oct 4 '07 #4
"dog" <do*@dog.comwrote in message news:fe**********@ss408.t-com.hr...
>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::strings;
obj()
{
s.push_back("bla");
s.push_back("Blaaa");
s.push_back("...");
}
};
Oct 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.