Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string array vector inside a class?

Reply
Thread Tools

string array vector inside a class?

 
 
kungfuelmosan
Guest
Posts: n/a
 
      11-26-2007
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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-26-2007
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
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      11-26-2007
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
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      11-26-2007
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
 
Reply With Quote
 
kungfuelmosan
Guest
Posts: n/a
 
      11-26-2007
Awesome! Thanks for the replies
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Uniform vector class, inheriting from Array: How to make sure thatmethods return a Vector and not an Array? Thomas Ruby 7 05-23-2005 04:21 PM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments