2011-09-18 22 views
2

我在C++ [非gui]中创建了一个文本编辑器,到目前为止我已经结束了这个代码.. 我得到两个未声明的错误...为什么?在C++中创建文本编辑器而不使用任何图形库[功能未声明的错误]

#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; 
    } 
} 

但是,在编译时出现错误[代码块生成日志]:

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 

回答

4

你的主要功能是看不到的编辑和开放的功能,因为他们后主出现。你可以通过以下任一方式解决这个问题:

1)移动主要上方的编辑和打开函数;或

2)添加编辑原型并打开主体。前主加入这行代码,但使用空间std后:

void edit(); 
void open(); 
+1

这意味着要么1)或2) - 你不需要这两个。 – TonyK

+0

谢谢,我编辑它使其更加清晰。 – bstamour

+1

用你的''''和我''或',我们完成了这件事:-) – TonyK

0

您需要声明的符号(函数,变量等),然后再使用它们。要解决您的问题,请向前申报您的功能。

#include <...> 
using namespace std; 

void edit(); 
void open(); 

int main() 
{ 
    // ... 
} 

void open() 
{ 
    // ... 
} 

void edit() 
{ 
    // ... 
} 
1

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

现在你有两个选择,要么把maineditopen下,或者写一个所谓的向前声明:

void edit(); 
void open(); 

这基本上是没有它的身体,你具备的功能。请注意,当你有多个源文件时,这种东西就是.h文件(标题)。

相关问题