Undefined reference to class function in simple ToDo app
After thoroughly debugging and trying different solutions, I am super confused as to why I am getting a “undefined reference to `SaveSystem::SaveTasks(std::vector<Task, std::allocator<Task> >)' ” error in my simple ToDo app. I am not unfamiliar with programming and debugging, but am new to C++ and CLion.
I declare the variable ‘MainSave’ of custom class type ‘SaveSystem’ here in SaveSystem.h:
class SaveSystem {
std::filesystem::path save_directory = "Saves";
std::filesystem::path save_file = save_directory / "Save.txt";
std::ofstream SaveFile();
public:
bool SaveFileExists();
bool SaveTasks(std::vector<Task> tasks);
bool LoadSaveFile();
std::filesystem::path GetSaveDirectory();
std::filesystem::path GetSaveFileName();
SaveSystem() = default;
};
#endif //TODOCPP_SAVESYSTEM_H
inline SaveSystem MainSave;The corresponding .cpp file “SaveSystem.cpp” looks like this:
#include "SaveSystem.h"
#include <filesystem>
#include <iostream>
#include <fstream>
bool SaveFileExists() {
if (!std::filesystem::exists(MainSave.GetSaveDirectory())) {
return false;
}
return true;
}
bool InitSaveSystem() {
if (SaveFileExists() == false) {
std::filesystem::create_directory(MainSave.GetSaveDirectory());
std::cout << "New directory path created for save files." << std::endl;
std::ofstream SaveFile(MainSave.GetSaveFileName());
}
else {
std::cout << "Save directory and file already exist." << std::endl;
}
return true;
}
bool SaveTasks(std::vector<Task> tasks) {
std::ofstream temp_save_file(MainSave.GetSaveFileName());
if (temp_save_file.is_open()) {
Task looping_task;
for (std::vector<Task>::iterator it = tasks.begin(); it != tasks.end(); ++it) {
looping_task = *it;
temp_save_file << looping_task.get_task_title() << " | " << looping_task.get_task_assignee() << std::endl;
}
}
else {
return false;
}
return true;
}
The file and code where the error is coming from says:
/home/user/CLionProjects/ToDoCPP/main.cpp:110:(.text+0x69a): undefined reference to `SaveSystem::SaveTasks(std::vector<Task, std::allocator<Task> >)'and main.cpp, specifically around the error:
....
else if (UserChoice == 4) {
MainSave.SaveTasks(ActiveTasks);
}
}After a couple of hours of trying to make it work, feeling like I got close, but ultimately not resolving this error, I turn to the community to please help me figure this out.
The goal of the code is to be able to save user-created tasks to a .txt file so that we can store the created tasks between user sessions.
Please sign in to leave a comment.