繁体   English   中英

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

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

我正在用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;
    }
}

但是,在编译时出现错误[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

您的主要功能看不到编辑和打开功能,因为它们出现在主要功能之后。 您可以通过以下任一方法解决此问题:

1)将编辑和打开功能移至主机上方; 要么

2)添加一个编辑原型并在main上方打开。 在main之前但在使用命名空间std之后添加以下代码行:

void edit();
void open();

C ++编译器是单程编译器。 这意味着它从上到下读取并翻译您的代码。 如果您使用的是函数(或任何其他符号),则编译器应在知道函数之前就知道该函数。

现在,您有两个选择,要么将main置于editopen ,要么编写所谓的前向声明:

void edit();
void open();

基本上,这是您没有机身即可拥有的功能。 请注意,当您有多个源文件时,.h文件(标头)中将放置这种内容。

您需要在使用符号之前声明它们(函数,变量等)。 要解决您的问题,请先声明您的功能。

#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