string array vector inside a class?

Discussion in 'C++' started by kungfuelmosan, Nov 26, 2007.

  1. Hey guys, Im just getting into c++ at the moment so please bare with
    me

    Basically i need to declare a vector<string> stringArray(50)
    inside a class, however by doing so i am getting the following error:

    error: expected identifier before numeric constant
    error: expected ';' or '...' before numeric constant

    But i can declare vector<string> testArray(50); inside my main
    function or a method function etc fine? it seems like i cant specify
    the (50) to make it an array

    My code is something like this:

    #include <vector>
    #include <string>

    using namespace std;

    class testClass {
    vector<string> testArray(50);
    }

    Cheers!

    Dan
    kungfuelmosan, Nov 26, 2007
    #1
    1. Advertising

  2. kungfuelmosan

    Kai-Uwe Bux Guest

    kungfuelmosan wrote:

    > Hey guys, Im just getting into c++ at the moment so please bare with
    > me
    >
    > Basically i need to declare a vector<string> stringArray(50)
    > inside a class, however by doing so i am getting the following error:
    >
    > error: expected identifier before numeric constant
    > error: expected ';' or '...' before numeric constant
    >
    > But i can declare vector<string> testArray(50); inside my main
    > function or a method function etc fine? it seems like i cant specify
    > the (50) to make it an array


    Right. That would be initialization not declaration.

    > My code is something like this:
    >
    > #include <vector>
    > #include <string>
    >
    > using namespace std;
    >
    > class testClass {
    > vector<string> testArray(50);
    > }


    Try:

    class testClass {

    // declare the variable
    vector< string > testArray;

    public:

    testClass ()
    // initialize the variable
    : testArray (50)
    {}

    };


    Note: std::vector<> is useful when the length may vary during the lifetime
    of the vector. If you are dealing with a fixed-length array, you might want
    to have a look into tr1::array.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 26, 2007
    #2
    1. Advertising

  3. kungfuelmosan a écrit :
    > Hey guys, Im just getting into c++ at the moment so please bare with
    > me
    >
    > Basically i need to declare a vector<string> stringArray(50)
    > inside a class, however by doing so i am getting the following error:
    >
    > error: expected identifier before numeric constant
    > error: expected ';' or '...' before numeric constant
    >
    > But i can declare vector<string> testArray(50); inside my main
    > function or a method function etc fine? it seems like i cant specify
    > the (50) to make it an array
    >
    > My code is something like this:
    >
    > #include <vector>
    > #include <string>
    >
    > using namespace std;
    >
    > class testClass {
    > vector<string> testArray(50);


    The parameters are specified in the constructor not in the declaration.

    > }


    Your class should be along:
    class testClass {
    //declaration
    vector<string> testArray.

    //constructor
    testClass ():testArray(50){
    }
    };

    Michael
    Michael DOUBEZ, Nov 26, 2007
    #3
  4. kungfuelmosan wrote:

    > Hey guys, Im just getting into c++ at the moment so please bare with
    > me
    >
    > Basically i need to declare a vector<string> stringArray(50)
    > inside a class, however by doing so i am getting the following error:
    >
    > error: expected identifier before numeric constant
    > error: expected ';' or '...' before numeric constant
    >
    > But i can declare vector<string> testArray(50); inside my main
    > function or a method function etc fine? it seems like i cant specify
    > the (50) to make it an array
    >
    > My code is something like this:
    >
    > #include <vector>
    > #include <string>
    >
    > using namespace std;
    >
    > class testClass {
    > vector<string> testArray(50);
    > }


    The type you actually have to use is vector<string>. Note that this type is
    designed to work without prior knowledge about the number of elements that
    should be stored inside it. So, in contrast to array declarations, you don't
    have to (and cannot) specify the number of elements you want to use. Your member
    must thus be declared so:
    class testClass {
    vector<string> testArray;
    }

    The statement
    vector<string> testArray(50);
    is both a declaration of a variable and an initialization: The constructor
    std::vector::vector (size_type _N, [+other parameters with default values])
    is called. In class declarations you are only allowed to _declare_ things, you
    must not try to _initialize_ them (initialization of members is what
    constructors are for).

    If you already know that your class only ever needs to hold 50 strings, you can
    use a plain array of strings:
    class testClass {
    string testArray[50];
    }
    Note that we use square brackets in this _declaration_ of an array type. It may
    be a bit confusing that the number of elements are put after the variable name,
    so that this declaration looks similar to "vector<string> testArray(50);",
    but actually the "[50]" part belongs to the type of the variable. It would be
    more obvious if we had to write "string[50] testArray;", but this doesn't work
    in C++ (for historical reasons).

    Another way would be do initialize your vector of strings in the constructor:
    class testClass {
    vector<string> testArray;
    testClass ();
    }

    testClass::testClass ()
    : testArray (50)
    {
    // Now testArray will have 50 entries.
    }

    Regards,
    Stuart
    Stuart Redmann, Nov 26, 2007
    #4
  5. Awesome! Thanks for the replies
    kungfuelmosan, Nov 26, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,782
  2. Replies:
    8
    Views:
    1,914
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. Thomas
    Replies:
    7
    Views:
    315
    Mark Hubbart
    May 23, 2005
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    357
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page