gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:57:29 24 4
gpt4 key购买 nike

我正在构建一个类,其中该类的构造函数采用表示日期的字符串。构造函数应将月、日和年分配给类的适当数据成员。

到目前为止,我已经写了一些非常基础的东西,只假定了几种类型的日期格式。

我的问题是我想使用用于构造函数参数的字符串。我想在类的主体中使用该字符串,但是当我使用它时,无论它在哪里使用,都会出现未声明的标识符错误。

我怎样才能避免这种情况?

类代码:

#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

构造函数代码:

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

编辑::

我使用将所有初始化都放在构造函数中的建议编辑了我的代码。我认为这与提出的其他方法本质上是一样的。

类代码:

#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

构造函数代码:

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

主要内容:

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

我收到以下错误:

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)

最佳答案

变量 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_sindyear< 中使用时可能仍未初始化 初始值设定项。因此,这可能不会让您一路走来,但这是一个好的开始。

关于c++ - 在类中使用未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18661546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com