How to declare array of strings & types <C++11

For Visual Studio 2010 I need to create two arrays which will be saved in class.
One array should contain strings like


1
2
3
4
5
// declaration:
const char* predefinedArgs;
// implementation:
this->predefinedArgs[] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", 
    "Aug", "Sep", "Oct", "Nov", "Dec" };

(this code does not work) Also I cannot to join it to one line because I don't use C++11.

And the second array which I need is to create array of times int and double
something like
1
2
3
4
const char* predefinedArgTypes;
// implementation:
this->predefinedArgTypes[] = { int, int, int, int, double, double, int, 
    int, int, int, int, std::string }; // the last one is just example for string 



How to solve it?
Sounds like you want a dynamic array in the first instance. I'd suggest using the standard library vector and string types.

Unfortunately, if you're pre-C++11 then you don't have initialiser list support, so you'd not be able to do it in a one-er. You can create a vector with an initial set of values from an array pretty easily, though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <string>

int main() 
{
    // C++11 (no auto here as we'll get const char* instead of std::string)
    std::vector<std::string> someStrings = { "Here", "Are", "Some", "Strings" };
    
    for (const auto& s : someStrings)
    {
        std::cout << s << std::endl;
    }
    
    // C++98
    typedef std::vector<std::string> TStringVector;
    std::string stringsAsArray[] = { "Some", "Other", "Strings" };
    std::vector<std::string> someOtherStrings (stringsAsArray, stringsAsArray + sizeof(stringsAsArray) / sizeof(stringsAsArray[0]));
    
    for (TStringVector::const_iterator it = someOtherStrings.begin(); it != someOtherStrings.end(); ++it)
    {
        std::cout << *it << std::endl;
    }
    
    return 0;
}


The second isn't possible - you can't mix types in arrays/containers.
Last edited on
However it is not possible to declare zero size array like this

header:
1
2
3
std::string predefinedArgs[]; // cannot declare
nor this
std::string stringsAsArray[] = { "Some", "Other", "Strings" }; // this one is declaration + initiation 


1
2
3
// header
I can do this:
std::string predefinedArgs[12]; // declare 

but not this:
1
2
3
4
// implementing constructor
this->predefinedArgs[12] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", 
    "Aug", "Sep", "Oct", "Nov", "Dec" };

error: : 'Wrapper::{ctor}' : constructors not allowed a return type
Which is why I recommend using a vector. Otherwise you have to start dynamically allocating your arrays.

Assuming you're creating some class: https://ideone.com/K8UCir

Edit: At this point I also feel obligated to share my feelings on how terrible I think the VS compiler is (very) and how much better life is when you migrate to a competent one (clang).
Last edited on
I am not able to use your vector example. In VS2010, the second example not the C++11. I am separating into header and cpp

Because I cannot do this
std::string stringsAsArray[] = { "Some", "Other", "Strings" };
must separate to header and cpp

I can do it in main:
1
2
3
4
5
6
int main(int argc, char ** argv)
{	
	int aaa = CV_MINOR_VERSION;
	std::string stringsAsArray[] = { "Some", "Other", "Strings" };
return 0;
}

BUt this is not what I wanted. Get rid of it from here and move it to .h and .cpp into a class
How to do it?

I could also move it to global scope:
1
2
3
4
std::string stringsAsArray[] = { "Some", "Other", "Strings" };

int main(int argc, char ** argv)
{	


But that is wrong practice.
Last edited on
See the above IDE one link (which uses a std::string array).

Also, it's totally possibly to do with vectors. Just not in a one-liner initialiser list.
Last edited on
Anyhow thanx, if you do not have perfect solution I will use global.
error: : 'Wrapper::{ctor}' : constructors not allowed a return type

based on that and some other hints (like the claim that "std::string stringsAsArray[] = { "Some", "Other", "Strings" };" does not compile), I am guessing that you need help writing a constructor that initializes a class member of type std::vector<std::string>.. but none of the code shown even shows a class, a constructor, or a member.
Is your array part of a class? If so do you want it to be const? If so then perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>
// Header file.
class Test
{
    public:
        static const std::string Args[] ;
};

// In your implementation file in the global scope.
const std::string Test::Args[] = {"string", "test"} ;


int main()
{
    Test my_test;

    for(int i = 0; i < 2; ++i)
        std::cout << Test::Args[i] << ", ";

    std::cout << std::endl;
}


Of course you'll need to include the proper header files in the appropriate files.

I removed that error. It was from nowhere just sudden like a smack. I had to undo to remove that cr*p.
The request is to separate it to declaration and to implementation.

std::string predefArgs[] = { "Some", "Other", "Strings" };

I cannot use it like this. I cannot to put it either to myclass.h nor to myclass.cpp
both will print error. Don't u use classes?
Last edited on
Don't u use classes?

Yes I use classes, however I'm not stuck in the past using C++98. I suggest you update your compiler and start using the current standards. Also you didn't really post enough code to decipher that you were using a class.

By the way the code I provided compiles with a C98 compiler.
I cannot separate it. I use Windows XP that is Visual Studio. No update.
Why not? It works for me. Show exactly what you've tried.

I use Windows XP that is Visual Studio. No update.

Okay since you're using an outdated Operating system, your stuck with an outdated compiler, good luck.

Last edited on
h4ever wrote:
std::string predefArgs[] = { "Some", "Other", "Strings" };

I cannot use it like this. I cannot to put it either to myclass.h nor to myclass.cpp
both will print error.

As shown, it will "not print error" in any revision of C++. You're not showing enough code for anyone to reproduce the problem you're experiencing.

Don't u use classes

I *suspect* what is actually happening is you're trying to compile code like this:

1
2
3
4
5
#include <string>

class X {
   std::string predefArgs[] = { "Some", "Other", "Strings" }; // error
};



A C++98 version of that would be
1
2
3
4
5
6
7
8
9
#include <string>
#include <vector>
class X {
   std::vector<std::string> predefArgs;
   X() { 
       const char* init[] = { "Some", "Other", "Strings" };
       predefArgs.assign(init, init + sizeof init/sizeof *init);
   }
};


and given
h4ever wrote:
The request is to separate it to declaration and to implementation.


1
2
3
4
5
// myclass.h
class X {
   std::vector<std::string> predefArgs;
   X();
};

and
1
2
3
4
5
// myclass.cpp
X::X() { 
    const char* init[] = { "Some", "Other", "Strings" };
    predefArgs.assign(init, init + sizeof init/sizeof *init);
}
I told you I am trying to separate declaration from implementation

so no

class X {
std::string predefArgs[] = { "Some", "Other", "Strings" }; // error
};

Last edited on
I'm not sure what more you want here. There have been solutions to your problem all through this thread.

Cubbi has given a perfectly succinct solution using vectors and a tidy way of initialising in your constructor. Not sure why you're not taking that.

If you're absolutely desperate to avoid vectors, I gave a solution in the fourth post that details how to do something similar with string arrays.

You seem to want a stack created array of strings that you can declare in one place, and implement in another in a one-liner. You can't. If you want this you need to heap allocate like I gave you in the example in the fourth post. If you want to stack create then you'll need to assign the string at each index individually - you cannot use an initialiser list.

My advice, aside from having a slightly better attitude towards those helping you, would be to take the eight lines of code offered by Cubbi at the end of his post that adequately solves your problem.

Last edited on
Topic archived. No new replies allowed.