class Days
{
public:
Days();
~Days(){};
void setTheDay(int day);
void printTheDay();
string returnTheDay();
string returnTheNextDay();
string returnThePreviousDay();
string addDays();
string currentDay[]; //is this correct?
};
Days::Days()
{
currentDay[7] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
} // is this correct?
The errors: c:\users\corey\documents\visual studio 2008\projects\daytypes\daytypes\days.h(23) : error C2059: syntax error : '{' c:\users\corey\documents\visual studio 2008\projects\daytypes\daytypes\days.h(23) : error C2143: syntax error : missing ';' before '{' c:\users\corey\documents\visual studio 2008\projects\daytypes\daytypes\days.h(23) : error C2143: syntax error : missing ';' before '}' I'm sure this is a common problem with people.
🔒 Locked
string array within a Class
Advertisement
Original Post
Help! I'm totally stumped and can't find an answer anywhere. I just want a simple array of strings inside of a class. I have using namespace, include ... look...
Make life easy on yourself and use an std::vector.
For example:
For example:
class Days{public: Days(); ~Days(){}; void setTheDay(int day); void printTheDay(); string returnTheDay(); string returnTheNextDay(); string returnThePreviousDay(); string addDays(); std::vector<string> currentDay;};Days::Days(){ currentDay.push_back( "sunday" ); currentDay.push_back( "monday" ); // etc...}
Before you take Mike's advice you should also understand how your approach would be done.
Basically you define the string array as a class member by simply defining a pointer which will be used to point to the beginning of the array.
string *currentDay;
Then you initialize it as follows:
currentDay = new string[7];
currentDay[0] = "sunday";
currentDay[1] = ...
Alternatively if you know there will always be 7 elements then you can simply declare the array as follows:
string currentDay[7];
And then fill the elements:
currentDay[0] = "sunday";
currentDay[1] = ...
If you really always need 7 elemnts then I wouldn't use std::vector. Instead use the static approach and exchange the 7 with a preprocessor definition. If you will change the size then use std::vector.
Basically you define the string array as a class member by simply defining a pointer which will be used to point to the beginning of the array.
string *currentDay;
Then you initialize it as follows:
currentDay = new string[7];
currentDay[0] = "sunday";
currentDay[1] = ...
Alternatively if you know there will always be 7 elements then you can simply declare the array as follows:
string currentDay[7];
And then fill the elements:
currentDay[0] = "sunday";
currentDay[1] = ...
If you really always need 7 elemnts then I wouldn't use std::vector. Instead use the static approach and exchange the 7 with a preprocessor definition. If you will change the size then use std::vector.
Quote:
Original post by induster
Okay, good advice. New to the vector class but this is looking good so far. It looks like I push all the days into the vector and it then acts like an array (with regards to indexing)...
That's correct.
A vector is an array under the hood, but it comes loaded with an interface to simplify all the things you might normally want to do with a regular array.
Prior to pushing all the days of the week onto the vector it is possible to hint how many elements you're going to push, this can allow the vector to optimise it's memory usage. You can do this using the reserve function:
Days::Days(){ currentDay.reserve(7); currentDay.push_back( "sunday" ); currentDay.push_back( "monday" ); // etc...}Whether or not calling reserve in this case will actually do anything at all is up to the vector, but it won't hurt, it can only benefit [smile].
Quote:
Original post by CProgrammer
Instead use the static approach and exchange the 7 with a preprocessor definition.
Well, assuming you're suggesting a pre-processor macro, then that would be the C approach (you're name suggests why you defaulted to this [smile]) but in C++ it's more natural to use a constant as they have static linkage.
The array-based approach is possible. However, as it has been presented so far, it is not advisable to use it. A better idea when using arrays:
// An array of constants. Easier to define this way.static const char* days[] = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};// ==================================================================// The "beginning" and "end" of an array.template<typename T, size_t N> T* begin(T (&a)[N]) { return a; }template<typename T, size_t N> T* end (T (&a)[N]) { return a + N; }// Inside a constructor: vector-based version.vector.assign(begin(days), end(days));// ==================================================================// Check if two arrays have the same size at compile-timetemplate<typename T1, typename T2, size_t N>void same_length(T1 (&)[N], T2 (&)[N]) { }// Inside a constructor: array-based version.same_length(days, array);std::copy(begin(days), end(days), begin(array));Quote:@The OP: This is close, but don't use the preprocessor for this; use a 'real' constant instead. (Search the forum archives for 'C++ define constants' for more information on this.)
If you really always need 7 elemnts then I wouldn't use std::vector. Instead use the static approach and exchange the 7 with a preprocessor definition. If you will change the size then use std::vector.
Also, consider using boost::array for fixed-size arrays. It offers some advantages over built-in fixed-sized arrays, including (but not limited to) range-checked access, and SC++L-container-like syntax (which makes it easier to swap it out with another container type if needed).
Thanks for all the responses. I learned a lot from this.
Basically in a class definition it just needs to have a pointer to the arrays first element... I'm not sure why it won't allow string myArray[10] there though. It would compile but all the data was garbage.
After some digging and realize I was missing a rule with classes. Members can't initialized inside of a class definition unless they are static constant.
Basically in a class definition it just needs to have a pointer to the arrays first element... I'm not sure why it won't allow string myArray[10] there though. It would compile but all the data was garbage.
After some digging and realize I was missing a rule with classes. Members can't initialized inside of a class definition unless they are static constant.
Quote:
Original post by dmatter
Well, assuming you're suggesting a pre-processor macro, then that would be the C approach (you're name suggests why you defaulted to this [smile]) but in C++ it's more natural to use a constant as they have static linkage.
Point taken :) Use the C++ constant.
I really should get my name changed at some point though. I haven't done much C programming in years :)
Quote:
Thanks for all the responses. I learned a lot from this.
Basically in a class definition it just needs to have a pointer to the arrays first element... I'm not sure why it won't allow string myArray[10] there though. It would compile but all the data was garbage.
After some digging and realize I was missing a rule with classes. Members can't initialized inside of a class definition unless they are static constant.
Thats the reason.
Basically, if you want to initialize non-static member variables you can do this in the constructor. static variables can be initialized directly since the required memory for the variable is allocated once the pogram starts an it remains allocated through the life of the process. Non-static ones are allocated in memory when a class instance is created thus you initialize them in the constructor.
Quote:
Original post by induster
Basically in a class definition it just needs to have a pointer to the arrays first element... I'm not sure why it won't allow string myArray[10] there though. It would compile but all the data was garbage.
NO.
Arrays and pointers are not the same thing. You need to have actual storage for the array. A pointer is only a pointer; it doesn't imply anything about what's pointed at.
The size of an array is part of the information about an array's type. When you write 'string currentDay[]' for your data member, you are actually specifying a pointer, which points nowhere in particular. 'string currentDay[7];', however, requests an array of 7 string elements, which is embedded directly into every class instance. It's not pointed-at; it's right there.
However, there is no reason to make a class for this. The fact that you want to make the class name plural is a good indication for this. Objects represent things. "The set of days in the week" is not a good example of a thing. You can't meaningfully talk about comparing two sets-of-days-in-the-week, because there is only one such set.
But even then, please understand that there (a) is no point in including a class name in the names of its member functions (it's simply redundant), and (b) no reason to put 'return' in function names in general, because returning stuff is normal behaviour for functions. And why on earth would you want to add days to the week?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement