Sign in to follow this  

[C++]Array of strings

This topic is 3501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want an array of 5 std::strings. I tried declaring like this: std::string str[5]; However my program is crashing when I try to access them: str[...].length(); The debugger says that there is apparently not an array of strings but it looks like only 1. What is the correct way of doing this? Thanks.

Share this post


Link to post
Share on other sites
I can't seem to get that working either. I've tried both of these:

struct ConvComponent
{
BYTE type;
std::vector <std::string> str;
BYTE val[5];
ConvComponent();
};

ConvComponent::ConvComponent()
{
for (int i = 0; i < 5; i++)
str.push_back( "" );
}

and

struct ConvComponent
{
BYTE type;
std::string str[5];
BYTE val[5];
};


In neither case, referring to the string: str[...] seems to work.

Share this post


Link to post
Share on other sites
I'm guessing you have more serious problems then. Are you allocating the ConvComponent by itself, or is it a member of some other class? If this is the case, is the "this" pointer of the calling function valid? If you have a pointer to a ConvComponent instance, is the pointer valid?

I would assume a minimal program works fine:

// Given that the array "val" has 5 elements
// Do you need a BYTE per std::string?
// Should you be using a vector of {byte, std::string} pairs?
struct ConvComponent
{
BYTE type;
std::vector <std::string> str;
BYTE val[5];
ConvComponent();
};

// This is the same as the loop version
// It default constructs 5 strings in the vector
// A default constructed string is the same as one
// initialised like std::string("");
ConvComponent::ConvComponent()
:
str(5)
{
}

int main()
{
ConvComponent component;
// do stuff with component
}




By reducing the set of things like this, you can rule out a lot of things. Once the above works, slowly start introducing any additional classes etc until it breaks. Then figure out what you are doing wrong with that part [smile]

Share this post


Link to post
Share on other sites
Hmm you're right... it is something more complicated (ConvComponent is in a vector, something is wrong with that it seems). Anyway debugging time...

Share this post


Link to post
Share on other sites
Please note that the STL string uses an overloaded subscript [i] to access the character at position i. Have you tried just creating 5 pointers to strings and instantiating them in the constructor?

Share this post


Link to post
Share on other sites

This topic is 3501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this