简体   繁体   English

在不使用任何图形库的情况下用c ++创建文本编辑器[此函数未声明错误]

[英]Creating a text editor in c++ without using any graphics library [the function is undeclared error]

I am creating a texteditor in c++[non gui] , So far i have ended up with this code.. i get two undeclared errors... Why? 我正在用c ++ [non gui]创建一个texteditor,到目前为止,我已经收到了这段代码。.我遇到了两个未声明的错误……为什么?

#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]: 但是,在编译时出现错误[CodeBlocks构建日志]:

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

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; 1)将编辑和打开功能移至主机上方; or 要么

2) Adding a prototype of edit and open above main. 2)添加一个编辑原型并在main上方打开。 Add this line of code before main, but after using namespace std: 在main之前但在使用命名空间std之后添加以下代码行:

void edit();
void open();

C++ compiler is a one pass compiler. C ++编译器是单程编译器。 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 under edit and open , or write a so-called forward declaration: 现在,您有两个选择,要么将main置于editopen ,要么编写所谓的前向声明:

void edit();
void open();

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. 请注意,当您有多个源文件时,.h文件(标头)中将放置这种内容。

You need to declare symbols (functions, variables, etc.) before you use them. 您需要在使用符号之前声明它们(函数,变量等)。 To fix your problem, forward declare your functions. 要解决您的问题,请先声明您的功能。

#include <...>
using namespace std;

void edit();
void open();

int main ()
{
    // ...
}

void open ()
{
    // ...
}

void edit ()
{
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM