I'm building a class where the constructor for the class takes a string representing a date. The constructor should assign the month, day and year into the appropriate data members of the class.
I've written something quite basic so far that assumes only a few types of date formats.
My problem is I would like to use the string that is used for the constructor argument. I want to use the string in the body of the class, but when I use it I get an undeclared identifier error wherever it used.
How can I prevent this?
Class code:
#ifndef CHRONO_H
#define CHRONO_H
#include <iostream>
#include <string>
class chrono {
public:
inline chrono(std::string s);
unsigned year;
unsigned month;
unsigned day;
std::string numyear{"0123456789"};
std::string alph{"abcdefghijklmnopqrstuvwxyz"};
std::string punc{",/"};
std::string::size_type indyear = s.find_first_of(punc);
std::string::size_type indmonth = s.find_first_of(alph);
std::string::size_type indmonthend = s.find_last_of(alph);
std::string::size_type lengthmonth = indmonthend - indmonth;
std::string::size_type inddate = s.find_first_of(numyear);
std::string::iterator begin = s.begin();
std::string::iterator end = s.end();
};
#endif
Constructor code:
#include <iostream>
#include <string>
#include "chrono.h"
inline chrono(std::string s) : year(s.substr(indyear,4)), month(tolower(s).substr(indmonth,lengthmonth)), day(s.substr(inddate,1)) {}
EDIT::
I edited my code using the suggestion to put all the initializations in the constructor. I think this is essentially the same thing as the other methods that were proposed.
Class code:
#ifndef CHRONO_H
#define CHRONO_H
#include <iostream>
#include <string>
class chrono;
class chrono {
public:
inline chrono(std::string s);
std::string numyear{"0123456789"};
std::string alph{"abcdefghijklmnopqrstuvwxyz"};
std::string punc{",/"};
std::string::size_type indyear, indmonth, indmonthend, lengthmonth, inddate;
std::string::iterator begin, end;
unsigned year;
unsigned month;
unsigned day;
};
#endif
Constructor code:
#include <iostream>
#include <string>
#include "chrono.h"
inline chrono::chrono(std::string s) : indyear(s.find_first_of(punc)), indmonth(s.find_first_of(alph)), indmonthend(s.find_last_of(alph)), lengthmonth(indmonthend - indmonth), inddate(s.find_first_of(numyear)), begin(s.begin()), end(s.end()), year(stoi(s.substr(indyear,4))), month(stoi(s.substr(indmonth,lengthmonth))), day(stoi(s.substr(inddate,1))) {}
Main:
#include <iostream>
#include <string>
#include "chrono.h"
int main()
{
std::string st;
std::cout << "Enter a date" << std::endl;
std::cin >> st;
chrono today(st);
std::cout << "Month " << today.month << std::endl;
std::cout << "Day " << today.day << std::endl;
std::cout << "Year " << today.year << std::endl;
return 0;
}
I get the following error:
Undefined symbols for architecture x86_64:
"chrono::chrono(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in ex9_51-JhoQAx.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
variable
s
is a parameter to your constructor, and only has scope-and-lifetime within that constructor.You're then trying to access it outside the constructor, in lines like:
If you want to keep
s
around, you need to store it in a member-variable:Finally, you need to reference
member_s
instead of parameters
NOTE I don't think this will solve all your problems, as I think
member_s
may still not be initialized when it is used in theindyear
initializer. So this may not get you all the way there, but its a good start.