i've writtn a code in c and it works well, so i tried to write the same code but in c++, ofcourse i need to change some functions to cpp, i did that, but when i run the program i got the follow error :

(a function-definition is not allowed here before '{' token)

i don't know why i got this error, so please if some 1 knows the problem, it'll be great.
the code below is not the all code, in the other files i have no problem just here. the error is at the second int(int CreateAllNodes)
here is the code:

#include "structure/main.h"
#include "linkedlist/linkedlist.h"
#include "iofile/iofile.h"


 ofstream *outfile;
 int main(int argc, char *argv[])
     {        
         ifstream *MyFile;
         char *programname ;


[COLOR="Red"] int CreateAllNodes(Link **Dhead, Link **Ahead[/COLOR])
   {


          char WordBuffer[24];   
          long iEndFile, iFSize;


         programname = argv[0];
         if(argc !=5)                                                           //Arguments asked to run the program
         error(1, "Gebruik: %s  -i file1 -o file2\n", programname);             // error if the input of the arguments false 
         if ((MyFile.is_open(argv[3,1,2], "r"))==NULL)                          // read file
         error(2, "can't open %s\n", argv[1]);                                  
         if((outfile.is_open(argv[1,3,4],"wb+"))==NULL)                         //write to the output file
         error(3, "can't open %s\n", argv[2]); 


         fseek(MyFile, 0L, SEEK_END);
         iEndFile = ftell(MyFile);                                  // the end of the file is the size of the file
         iFSize = iEndFile;
         rewind(MyFile);

      while (ftell(MyFile) < iEndFile)
         {


        cin >> MyFile >>" " >> WordBuffer;


        if (WordBuffer[0] == 'Z' || WordBuffer[0] == 'Z')
        {
            AddToList(Dhead, WordBuffer);
        }
        else
        {
            AddToList(Ahead, WordBuffer);
        }

    }

    MyFile.close();
    return 0;
 }     


    Link *Dhead = NULL;  // caps on struct and deleted struct key word
    Link *Ahead = NULL;

    //call function to creat linked list nodes  
    CreateAllNodes(&Dhead, &Ahead);   // used the &


    WalkNode(Ahead);
    WalkNode(Dhead);


    //Delete lists using DestroyAllNodes call; call twice, once for each list
    DestroyAllNodes(Ahead);
    DestroyAllNodes(Dhead);

    cout<<"\n\tdone with the program\n\n";
    exit(EXIT_SUCCESS);
 }

Recommended Answers

All 5 Replies

I'm not a C programmer but why are you declaring a function inside your main?
For example:

void number()
{
 // function content
}

int main()
{

   number();

}

Not:

int main()
{
   void number()
   {
    // function content
   }
}

Functions cannot be nested in C++. They have to be at global, namespace, or class scope. You should be able to fix the error by moving the definition of CreateAllNodes() so that it comes before the definition of main().

>>ifstream *MyFile;
Remove the * -- c++ streams are not normally pointers.

>>programname = argv[0];
argv is not a variable declared in t CreateAllNodes() function I think you put that function declaration in the wrong place.

should i use the (std::ifstream MyFile)?
i've used the form but i've got an error.

i tried to put the function of CreateAllNodes() everywhere, but still the same, couldn't open the text file

well, i'll try to build the code with classes. maybe i'll successed:icon_rolleyes:

Here is how to open the file in main(), assuming the filename is in argv[1].

#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
    ifstream in(argv[1]);

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.