Use of undeclared identifier in a class

2.8k views Asked by At

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

There are 3 answers

0
abelenky On

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:

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

If you want to keep s around, you need to store it in a member-variable:

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
   { }

Finally, you need to reference member_s instead of parameter s

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

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 the indyear initializer. So this may not get you all the way there, but its a good start.

1
cpp On

You need class identifier:

inline chrono::chrono(std::string s) : year(s.substr(indyear,4)), month(tolower(s).substr(indmonth,lengthmonth)), day(s.substr(inddate,1)) {}
5
LihO On

"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"

In your constructor, the s is a temporary copy with automatic storage duration that exists only within the scope of this constructor. To solve this you can define a new member of this class and initialize it using the parameter that has been passed to the constructor. Also consider using #define (or public static const members) for strings that will never change in the runtime:

#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; 
}

Also note that you've defined integral members year, month and day, yet you initialize them as std::string objects.