473,406 Members | 2,371 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,406 software developers and data experts.

string array vector inside a class?

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

Basically i need to declare a vector<stringstringArray(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<stringtestArray(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<stringtestArray(50);
}

Cheers!

Dan

Nov 26 '07 #1
4 3408
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<stringstringArray(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<stringtestArray(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<stringtestArray(50);
The parameters are specified in the constructor not in the declaration.
}
Your class should be along:
class testClass {
//declaration
vector<stringtestArray.

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

Michael
Nov 26 '07 #2
kungfuelmosan wrote:
Hey guys, Im just getting into c++ at the moment so please bare with
me

Basically i need to declare a vector<stringstringArray(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<stringtestArray(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<stringtestArray(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
Nov 26 '07 #3
kungfuelmosan wrote:
Hey guys, Im just getting into c++ at the moment so please bare with
me

Basically i need to declare a vector<stringstringArray(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<stringtestArray(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<stringtestArray(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<stringtestArray;
}

The statement
vector<stringtestArray(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<stringtestArray(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<stringtestArray;
testClass ();
}

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

Regards,
Stuart
Nov 26 '07 #4
Awesome! Thanks for the replies
Nov 26 '07 #5

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

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
8
by: Sowen | last post by:
Hi, I have an object "elem", there are only simple functions inside, like setName, getName, and three constructors Now I have another class "Base", need an array of elem to initialize class...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
3
by: Vij | last post by:
I can do this int a = { 5,6,7,8,9}; but how can I do this inside a class? something like this class CTest { private: int a // Init the array here };
7
by: Felix85 | last post by:
I am trying to make a command interpreter for a mud that i am working on the problem i am having right now is that i cannot convert the string into a char array. This is the error I am getting...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
3
by: Spoon | last post by:
Hello everyone, I want to create an array of objects at run-time. AFAIU, operator new will call the default constructor for each object in the array. In other words, the following program will...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.