开发者

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

开发者 https://www.devze.com 2023-04-05 21:45 出处:网络
I am creating a texteditor in c++[non gui] , So far i have ended up with this code.. i get two undeclared errors... Why?

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


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:

void edit();
void open();


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 under edit and open, or write a so-called forward declaration:

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.


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 ()
{
    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号