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!
You need to define the static member in a source file of your program, outside of the class definition.
E.g. in a .cpp: