gpt4 book ai didi

C++ 单独的头文件和源文件

转载 作者:行者123 更新时间:2023-11-27 23:56:45 24 4
gpt4 key购买 nike

我是 C++ 的新手,到目前为止,我已经将所有代码都放在同一个文件中。现在,随着我的进步,我需要将我的代码分成我不太熟悉的源文件和头文件。

我可以让它处理简单的任务,但是在我现在尝试分成单独文件的这个程序中给我一个错误,而当我把它全部放在一个文件中时,我可以编译它。

我卡在错误信息上了

main.cpp:10:1: error: unknown type name 'textEditor'
textEditor siEditor;

如果有人能解释为什么我会遇到这个错误以及如何防止它,我们将不胜感激。我读到它可能与重复声明有关,但我不明白从哪里来的。

这是我的 main.cpp 的样子:

#include <iostream>
#include <fstream>

using namespace std;

#include "textData.h"
#include "textEditor.h"

textData siData;
textEditor siEditor;

int main()
{
cout << "\nWelcome to siEdit!" << endl;
while (true)
{
cout << "\nWhat would you like to do? \nNew file = n, Append = a, View = v, Quit = q: ";
string toDo;
cin >> toDo;

if (toDo == "n")
{
siEditor.openText();
cout << "Now editing the file: " << siData.fileName.c_str() << endl;
cout << "Type '=!' to stop editing and save. \n " << endl;
siEditor.writeText();
}

else if (toDo == "a")
{
siEditor.appendTextOpen();
cout << "Now appending text: " << siData.appendTextfileName.c_str() << endl;
cout << "Type '=!' to stop editing and save changes. \n " << endl;
siEditor.appendText();
}

else if (toDo == "v")
{
siEditor.readText();
cout << "\n";
}

else if (toDo == "q")
{
return 0;
}

else
{
cout << "Invalid input." << endl;
}
}
}

siEdit.cpp:

#include <iostream>
#include <fstream>

using namespace std;

#include "textData.h"
#include "textEditor.h"

textData siData;

class textEditor
{
public:
void openText()
{
//when associated file is open.
while (siData.siFile.is_open())
{
siData.siFile.close();
}
cout << "\nWhat do you want to call your file? ";
cin >> siData.fileName;

//Creates / Opens fileEditor
const char* path = siData.fileName.c_str();
siData.siFile.open(path);
}

void writeText()
{
bool editing = true;
bool hasEditing = false;

while (editing == true)
{
//Get user input
string input = " ";
getline(cin, input);
string yesNo;

if (input == "=!")
{
cout << "Would you like to save the file? Y/N" << endl;
cin >> yesNo;

if (yesNo == "Y")
{
cout << "Filed saved: " << siData.fileName.c_str();
editing = false;
}

else if (yesNo == "N")
{
cout << "No changes have been saved. Exiting." << endl;
hasEditing = false;
editing = false;
siData.siFile.clear();
}

else
{
cout << "Invalid input. Type '=! to exit." << endl;

}
}

else
{
siData.siFile << input;
siData.siFile << endl;
hasEditing = true;
}
}
}


void readText()
{
string line;
cout << "\nEnter the name of your file: ";
cin >> siData.fileName;
cout << "\n";
const char* path = siData.fileName.c_str();

// input file stream
//Internal stream buffer which performes I/O on file.
ifstream siFileRead(path);
if(siFileRead.is_open())
{
while(getline(siFileRead,line))
{
cout << line << endl;
siData.siFile << line;
}
}

else
{
cout << "Unable to open file. Confirm name and file location.";
}
}

// open the existing text file
void appendTextOpen()
{
while (siData.siFileAppend.is_open())
{
// erase previous text
siData.siFileAppend.clear();
// close the input text file
siData.siFileAppend.close();
}

cout << "\nEnter the name of the file: ";
//find file name.
cin >> siData.appendTextfileName;

//Makes / Opens file
const char* path = siData.appendTextfileName.c_str();
siData.siFileAppend.open(path, fstream::app);
}

//add text together with previous input.
void appendText()
{
bool editing = true;
bool hasEditing = false;

while (editing == true)
{
//Gets user input
string input = " ";
getline(cin, input);

if (input == "=!")
{
if (hasEditing == true)
{
cout << "File saved: " << siData.appendTextfileName.c_str() << endl;
editing = false;
}
}

else
{
siData.siFileAppend << input;
siData.siFileAppend << endl;
hasEditing = true;
}
}
}
};

文本数据.h:

#ifndef SIEDITOR_H
#define SIEDITOR_H

class textData
{
public:
string fileName;
string appendTextfileName;
ofstream siFile;
ofstream siFileAppend;
};

#endif

文本编辑器.h:

#ifndef SIEDITOR_H
#define SIEDITOR_H

class textEditor
{
public:
void openText()
void writeText()
void readText()
void appendTextOpen()
void appendText()
};


#endif

最佳答案

您在两个头文件中使用相同的 include guard,即 SIEDITOR_H。这样可以防止包含第二个 header 的内容。使用 #pragma once 而不是包含保护符号。

#pragma once 是一个事实上的标准,即 supported by all compilers of practical interest .

在您的实现文件中不要重复类定义。只需定义声明的成员函数。和 static 数据成员,如果有的话。

关于C++ 单独的头文件和源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174253/

24 4 0
文章推荐: javascript - 在 Internet Explorer 中更改 css 后还原