title: Use of undeclared identifier in a class
body: <p>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.</p>
  
  <p>I've written something quite basic so far that assumes only a few types of date formats.</p>
  
  <p>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. </p>
  
  <p>How can I prevent this?</p>
  
  <p>Class code:</p>
  
  <pre><code>#ifndef CHRONO_H
  #define CHRONO_H
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  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
  </code></pre>
  
  <p>Constructor code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "chrono.h"
  inline chrono(std::string s) : year(s.substr(indyear,4)), month(tolower(s).substr(indmonth,lengthmonth)), day(s.substr(inddate,1)) {}
  </code></pre>
  
  <p>EDIT::</p>
  
  <p>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.</p>
  
  <p>Class code:</p>
  
  <pre><code>#ifndef CHRONO_H
  #define CHRONO_H
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  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
  </code></pre>
  
  <p>Constructor code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #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))) {}
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "chrono.h"
  int main()
  {
      std::string st;
      std::cout &lt;&lt; "Enter a date" &lt;&lt; std::endl;
      std::cin &gt;&gt; st;
      chrono today(st);
      std::cout &lt;&lt; "Month " &lt;&lt; today.month &lt;&lt; std::endl;
      std::cout &lt;&lt; "Day " &lt;&lt; today.day &lt;&lt; std::endl;
      std::cout &lt;&lt; "Year " &lt;&lt; today.year &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "chrono::chrono(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt;)", 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)
  </code></pre>