简体   繁体   English

在类中使用未声明的标识符

[英]Use of undeclared identifier in a class

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. 变量s是构造函数的参数,并且在该构造函数中只有范围和生命周期。

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: 如果你想保持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
   { }

Finally, you need to reference member_s instead of parameter s 最后,您需要引用member_s而不是参数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. 注意我认为这不会解决您的所有问题,因为我认为当在indyear初始化程序中使用member_s时,它仍然可能尚未初始化。 So this may not get you all the way there, but its a good start. 所以这可能不会让你一直到那里,但它是一个良好的开端。

你需要类标识符:

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

"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. 在构造函数中, s是具有自动存储持续时间的临时副本,该副本仅存在于此构造函数的范围内。 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 (或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; 
}

Also note that you've defined integral members year , month and day , yet you initialize them as std::string objects. 另请注意,您已经定义了成员yearmonthday ,但您将它们初始化为std::string对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM