在 c++ 中创建文本编辑器不使用任何图形库[函数未声明错误]
我正在用 c++[非 gui] 创建一个文本编辑器,到目前为止我已经得到了这段代码。 我收到两个未声明的错误...为什么?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int op;
cout<<"do you want to open example.txt or overwrite?\nIf you want to overwrite enter 1 , if you want to view it enter 2. :\n";
cin>>op;
if(op==1)
{
edit();
}
else if(op==2)
{
open();
}
}
void edit()
{
int op;
string x;
ofstream a_file("example.txt" , ios::app);
cout<<"HEY ENTER SOME TEXT TO BE WRITTEN TO EXAMPLE.txt [created by rohan bojja]\n--------------------------------------------------------------------------------\n";
getline ( cin , x);
a_file<<x;
cout<<"want to type in an other line?\n1 for YES, 2 for NO";
cin>>op;
while(op==1)
{
a_file<<"\n";
edit;
}
cout<<"Do you want to quit?\n1 for YES , 2 for NO";
cin>>op;
if (op==2)
{
edit;
}
}
void open()
{
int op;
ifstream a_file("example.txt");
cout<<"You are now viewing example.txt [created by rohan bojja]\n--------------------------------------------------------------------------------\n";
cout<<a_file;
cout<<"Do you want to quit?\n1 for YES , 2 for NO";
cin>>op;
if(op==2)
{
open;
}
}
但是,在编译时出现错误 [CodeBlocks Build Log]:
F:\Projects\c++\TextEditor\texteditor.cpp: In function 'int main()':
F:\Projects\c++\TextEditor\texteditor.cpp:14: error: 'edit' was not declared in this scope
F:\Projects\c++\TextEditor\texteditor.cpp:18: error: 'open' was not declared in this scope
I am creating a texteditor in c++[non gui] , So far i have ended up with this code..
i get two undeclared errors... Why?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int op;
cout<<"do you want to open example.txt or overwrite?\nIf you want to overwrite enter 1 , if you want to view it enter 2. :\n";
cin>>op;
if(op==1)
{
edit();
}
else if(op==2)
{
open();
}
}
void edit()
{
int op;
string x;
ofstream a_file("example.txt" , ios::app);
cout<<"HEY ENTER SOME TEXT TO BE WRITTEN TO EXAMPLE.txt [created by rohan bojja]\n--------------------------------------------------------------------------------\n";
getline ( cin , x);
a_file<<x;
cout<<"want to type in an other line?\n1 for YES, 2 for NO";
cin>>op;
while(op==1)
{
a_file<<"\n";
edit;
}
cout<<"Do you want to quit?\n1 for YES , 2 for NO";
cin>>op;
if (op==2)
{
edit;
}
}
void open()
{
int op;
ifstream a_file("example.txt");
cout<<"You are now viewing example.txt [created by rohan bojja]\n--------------------------------------------------------------------------------\n";
cout<<a_file;
cout<<"Do you want to quit?\n1 for YES , 2 for NO";
cin>>op;
if(op==2)
{
open;
}
}
but , while compiling i get the error [CodeBlocks Build Log]:
F:\Projects\c++\TextEditor\texteditor.cpp: In function 'int main()':
F:\Projects\c++\TextEditor\texteditor.cpp:14: error: 'edit' was not declared in this scope
F:\Projects\c++\TextEditor\texteditor.cpp:18: error: 'open' was not declared in this scope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 main 函数看不到编辑和打开函数,因为它们出现在 main 之后。您可以通过以下任一方法解决此问题:
1)将编辑和打开功能移至主函数上方;或者
2)在main上面添加edit和open的原型。在 main 之前、使用命名空间 std 之后添加这行代码:
Your main function can't see the edit and open functions because they appears after main. You can fix this by either:
1) Moving the edit and open functions above main; or
2) Adding a prototype of edit and open above main. Add this line of code before main, but after using namespace std:
C++ 编译器是一次性编译器。这意味着它从上到下读取并翻译您的代码。如果您正在使用函数(或任何其他符号),编译器应该在到达它之前知道它。
现在您有两个选择,要么将
main
放在edit
和open
下,要么编写一个所谓的前向声明:这基本上就是您要执行的函数 。有而无其身。请注意,当您有多个源文件时,此类内容会放入 .h 文件(标头)中。
C++ compiler is a one pass compiler. Which means it reads from top to bottom and translates your code. If you are using a function (or any other symbol), the compiler should know about it before it reaches it.
Now you have two options, either put
main
underedit
andopen
, or write a so-called forward declaration:That is basically the function you have without its body. Note that this kind of things are what is put in .h files (headers) when you have multiple source files.
在使用符号(函数、变量等)之前需要先声明它们。要解决您的问题,请转发声明您的函数。
You need to declare symbols (functions, variables, etc.) before you use them. To fix your problem, forward declare your functions.