0

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)
4

3 回答 3

3

变量s是构造函数的参数,并且仅在该构造函数中具有作用域和生命周期。

然后,您尝试在构造函数之外访问它,如下所示:

std::string::size_type indyear = s.find_first_of(punc);

如果要保留s,则需要将其存储在成员变量中:

class chrono {
public:
    inline chrono(std::string s);
    unsigned year;
    unsigned month;
    unsigned day;
    std::string  member_s;   // Here's the member-variable.
};

inline chrono(std::string s) :
    year(s.substr(indyear,4)),
    month(tolower(s).substr(indmonth,lengthmonth)),
    day(s.substr(inddate,1)),
    member_s(s) // Here we store the local-variable s in the member-variable
   { }

最后,您需要引用member_s而不是参数s

std::string::size_type indyear = member_s.find_first_of(punc);

注意我认为这不会解决您的所有问题,因为我认为member_s在初始化程序中使用它时可能仍未indyear初始化。所以这可能不会让你一路走好,但它是一个好的开始。

于 2013-09-06T15:45:49.023 回答
1

您需要类标识符:

inline chrono::chrono(std::string s) : year(s.substr(indyear,4)), month(tolower(s).substr(indmonth,lengthmonth)), day(s.substr(inddate,1)) {}
于 2013-09-06T15:46:43.307 回答
0

“我想使用用于构造函数参数的字符串。我想在类的主体中使用字符串”

在您的构造函数中,s是具有自动存储持续时间的临时副本,仅存在于此构造函数的范围内。要解决此问题,您可以定义此类的新成员并使用已传递给构造函数的参数对其进行初始化。还可以考虑将#define(或public static const成员)用于在运行时永远不会改变的字符串:

#define CHRONO_ALPH "abcdefghijklmnopqrstuvwxyz"
#define CHRONO_DIGITS "0123456789"
#define CHRONO_PUNC ",/"

class chrono {
public:
    chrono(std::string s_) :
        s(s_),
        indyear(s.find_first_of(CHRONO_PUNC)),
        indmonth(s.find_first_of(CHRONO_ALPH)),
        inddate(s.find_first_of(CHRONO_DIGITS)),
        indmonthend(s.find_last_of(CHRONO_ALPH)),
        lengthmonth(indmonthend - indmonth),
        year(s.substr(indyear,4)),
        month(tolower(s).substr(indmonth,lengthmonth)),
        day(s.substr(inddate,1)) { }

    std::string s, year, month, day;
    size_t indyear, indmonth, inddate, indmonthend, lengthmonth; 
}

另请注意,您已经定义了整数成员和year,但您将它们初始化为对象。monthdaystd::string

于 2013-09-06T15:47:59.143 回答