C++文件处理类

C++ file Handling class

本文关键字:处理 文件 C++      更新时间:2023-10-16

我发现我在整个程序中一次又一次地使用相同的代码。为了提高效率等,我决定实现一个文件处理类,它将允许我与所有文件交互。

在创建这个过程中,我遇到了一些我无法破解的奇怪错误。例如:

Error   11  error LNK1169: one or more multiply defined symbols found   C:UsersJGDesktopProjectWorkConsoleApplication1DebugConsoleApplication1.exe   1   1   ConsoleApplication1

Error   8   error LNK2005: "bool __cdecl bolFileExist(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?bolFileExist@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Draw.obj   C:UsersJGDesktopProjectWorkConsoleApplication1ConsoleApplication1Player.obj  ConsoleApplication1

我只能将其归结为Filez.h文件中的代码,作为对它构建并运行良好的所有相关代码的注释。我已经对此进行了一些重新研究,但遗憾的是,我一无所获。

我将非常感谢对这段代码的一些反馈,以及关于我做错了什么的一些建议。

string getFinalLineOfFile(string FileLoction)
{
    //http://bit.ly/1j6h6It
    string line = " ";
    string subLine;
    ifstream readFile(FileLoction);
    string found_DrawID; //Username in the file;
    while (getline(readFile,line)) {
    stringstream iss(line);
    //We are only Interested in the First Value 
        iss >> subLine;
    }
    //The Value at CurrentDrawID will be the final value in the file; 
    return subLine;

}
bool bolFileExist(string FileLocation)
{
    //If that Exists. Return it. 
    ifstream readFile(FileLocation);
    return readFile;
}
bool itemExistLineOne(int find, string FileLocation)
{
    string line = " ";
    //ifstream readFile(".//Draws//Draws.txt");
    ifstream readFile(FileLocation);
    string foundID; //Username in the file;
    while (getline(readFile,line)) {
    stringstream iss(line);
    iss >> foundID;
    //Covert the Integer input to a String for comparison.
    if (to_string(find) == foundID) {
            return true;
      } 
    }
    return false; 
}
void CreateNewFileLine(string Location, string text){
    ofstream output_file(Location, ios::app);
    if (!output_file.is_open()) { // check for successful opening
        cout << "Output file could not be opened! Terminating!" << endl;
    }
    else{
    output_file << text;
    output_file << endl; //Create new line at the end of the file. 
    output_file.close();
    }

}

非常感谢,

您可能在某些标头中缺少"inline":

struct X { void f(); };
inline void X::f() {} // will be multiply defined without inline.

该标头的末尾