簡體   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