我是C++的新手,到目前爲止我已經將所有代碼放在同一個文件中。 現在,隨着我的進步,我需要將我的代碼分離成我並不熟悉的源代碼和頭文件。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;
}
}
}
};
textData.h:
#ifndef SIEDITOR_H
#define SIEDITOR_H
class textData
{
public:
string fileName;
string appendTextfileName;
ofstream siFile;
ofstream siFileAppend;
};
#endif
textEditor.h:
#ifndef SIEDITOR_H
#define SIEDITOR_H
class textEditor
{
public:
void openText()
void writeText()
void readText()
void appendTextOpen()
void appendText()
};
#endif
您不應該在您的cpp文件中重新定義您的類;你應該實施這些方法。理想情況下,你應該有'textData.cpp'和'textEditor.cpp',否則我不認爲它會被正確包含。 – byxor
我認爲可能有工作示例的代碼拆分爲網頁上的標題和實現文件。 – juanchopanza