当尝试使用静态 ifstream 成员和附带函数时,出现链接器错误
假设如果在 .o 文件中发现错误,则表明这是链接器的问题...
无论如何,我正在编写一个程序来组织我想在大学学习的课程,将它们输出到格式良好的 HTML 文件中。为此,我将把 Course 类的一堆对象放入一个列表中(我选择的数据结构,除非有人另外警告),按课程代码组织它们,并将它们输出到前面提到的 HTML 文件中,将每个不同的主要/次要/等之间的水平换行符。每个断行部分的顶部将是专业的名称,读起来像:
AMS(应用数学和统计学)
每当用户输入课程代码(只是三个字母,而不是数字)时,我希望类检查代码是否位于我提供的 .txt 中程序。因为如果我最终要拥有几十个课程
,为什么要为每个对象使用 fstream
或成员函数来使我的代码变得臃肿呢?所以我认为使用一些静态魔法可能是个好主意。然后我收到此错误消息,使用 g++;
(抱歉,它看起来很乱,顺便说一句,我不知道如何在这里整齐地格式化它。)
在函数 `std::basic_ifstream >::open(char const*, std::_Ios_Openmode)' 中:
/usr/include/c++/4.5/fstream| 528 | 528对
Course::courses' 的未定义引用 /usr/include/c++/4.5/fstream|533|未定义的引用
Course::courses' /usr/include/c++/4.5/fstream|533|对Course::courses' 的未定义引用 obj/Debug/main.o 在函数中
std::basic_filebuf >::is_open() const':/usr/include/c++/4.5/fstream|223|对
Course::courses' 的未定义引用 obj/Debug/main.oIn 函数
std::basic_ifstream >::close()': /usr/include/c++/4.5/fstream|566|对Course::courses' 的未定义引用 obj/Debug/main.o:/usr/include/c++/4.5/fstream|529|更多未定义的引用
Course::courses' follow
我将被定向到 fstream 标头的这一段;
void open(const char* __s, ios_base::openmode __mode = ios_base::in)
{
if (!_M_filebuf.open(__s, __mode | ios_base::in))
this->setstate(ios_base::failbit);
else
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 409. Closing an fstream should clear error state
this->clear();
}
我拥有的两个静态成员(在课程的整个收集过程的开始和结束时打开和关闭流)以及成员本身是这样声明的;
class Course
{
public:
Course();
void setup();
bool confirm();
stringstream entry;
static void openCodeList() { courses.open("codes"); if (!courses.is_open()) exit(1); }
static void closeCodeList() { courses.close(); }
private:
//Irrelevant strings and bits, etc, etc.
static ifstream courses;
}
我确实涉及其他 .cpp 和 .h 文件(即最终处理实际输出所有内容的 File 类),但我什至没有在其中任何一个中提到这些与 fstream 相关的成员,但我显然是 WIP main.cpp;
int main()
{
cout << "Welcome to the Stony Brook Course Organizer! This program will help you\n"
"organize the courses that you wish to take at Stony Brook University.\n\n";
Course thefirst;
Course::openCodeList();
thefirst.setup();
thefirst.confirm();
Course::closeCodeList();
}
任何在处理我的静态
问题方面的帮助以及对未来的建议(即使与此没有直接关系)将不胜感激。谢谢你!
Assuming that if the error's spotted in the .o file that it's the linker's problem...
Anyway, I'm writing a program to organize the courses I want to take in college by outputting them into a nicely formatted HTML file. To do this, I'm going to throw a bunch of objects of my Course class into a list (the data structure I picked, unless someone warns otherwise), organize them by course code, and output them to the aforementioned HTML file, placing horizontal line breaks between each different major/minor/etc. At the top of each line-broken segment will be the name of the major, reading like;
AMS (Applied Mathematics and Statistics)
Whenever the user enters the course code (just the three letters, not the number), I want the class to check whether the code is on a .txt I provide with the program. Because if I'm going to have dozens of Courses
in the end, why bloat my code with an fstream
or a member function for each object? So I figured using some static
magic might be a good idea. Then I get this error message, using g++;
(Sorry it looks messy, by the way, I'm not sure how to format this neatly here.)
In function `std::basic_ifstream >::open(char const*, std::_Ios_Openmode)':
/usr/include/c++/4.5/fstream| 528 | undefined reference to
Course::courses'
Course::courses'
/usr/include/c++/4.5/fstream|533|undefined reference to
/usr/include/c++/4.5/fstream|533|undefined reference toCourse::courses'
std::basic_filebuf >::is_open() const':
obj/Debug/main.o In function/usr/include/c++/4.5/fstream|223|undefined reference to
Course::courses'
std::basic_ifstream >::close()':
obj/Debug/main.oIn function
/usr/include/c++/4.5/fstream|566|undefined reference toCourse::courses'
Course::courses' follow
obj/Debug/main.o:/usr/include/c++/4.5/fstream|529|more undefined references to
I'm then directed to this segment of the fstream header;
void open(const char* __s, ios_base::openmode __mode = ios_base::in)
{
if (!_M_filebuf.open(__s, __mode | ios_base::in))
this->setstate(ios_base::failbit);
else
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 409. Closing an fstream should clear error state
this->clear();
}
The two static members I have (open and close the stream, at the start and end of the whole collection process for the courses) as well as the member itself are declared like so;
class Course
{
public:
Course();
void setup();
bool confirm();
stringstream entry;
static void openCodeList() { courses.open("codes"); if (!courses.is_open()) exit(1); }
static void closeCodeList() { courses.close(); }
private:
//Irrelevant strings and bits, etc, etc.
static ifstream courses;
}
I do have other .cpp and .h files involved (namely a File class to handle actually outputting everything in the end), but I have not even mentioned these fstream-related members in any of them but my obviously WIP main.cpp;
int main()
{
cout << "Welcome to the Stony Brook Course Organizer! This program will help you\n"
"organize the courses that you wish to take at Stony Brook University.\n\n";
Course thefirst;
Course::openCodeList();
thefirst.setup();
thefirst.confirm();
Course::closeCodeList();
}
Any help in dealing with my static
issues and advice for the future (even if not directly related to that) would greatly be appreciated. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在程序的源文件中、类定义之外定义静态成员。
例如在 .cpp 中:
You need to define the static member in a source file of your program, outside of the class definition.
E.g. in a .cpp: