Error measage in compiling c++ program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 2
Reputation: wildkms725 is an unknown quantity at this point 
Solved Threads: 0
wildkms725 wildkms725 is offline Offline
Newbie Poster

Error measage in compiling c++ program

 
0
  #1
Feb 1st, 2005
error reads: body has already been defined for function 'main()'
program is as follows:
search.h file

  1. // display files in a directory
  2. #include <stdlib.h>
  3. #include <dir.h>
  4. #include <dos.h>
  5. #include <string.h>
  6. #include <iostream.h>
  7. #include <fstream.h>
  8.  
  9. ofstream outfile("output.dat",ios::out);
  10.  
  11. class dispfiles
  12. {
  13. public:
  14. int showfiles(void);
  15. char allfiles[MAXPATH];
  16. private:
  17. char *getdirectory(char *path);
  18. char curdir[MAXPATH],temp[3];
  19. struct ffblk fileinfo;
  20. int attrib,done;
  21. };

search.cpp file

  1. /*
  2. * Routine to hunt down a snippet of text inside
  3. * a file
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define MAXCHAR 2048 //number of characters to read from disk
  11.  
  12. void main(void)
  13. {
  14. char filename[256];
  15. char text[256];
  16. long x;
  17. char buffer[MAXCHAR+1];
  18. FILE *f;
  19. long matches = 0;
  20. char *a, *b;
  21.  
  22.  
  23. strcpy(filename, "c:\\textfile.txt"); // Get filename
  24. strupr(filename); //convert filename to U/C
  25.  
  26. if((f = fopen(filename, "r")) == NULL)
  27. {
  28. printf("Error opening %s\n", filename);
  29. exit(0);
  30. }
  31. //Get search string
  32. strcpy(text,"COMPUTER");
  33.  
  34. printf("\nLooking in %s for \"%s\" . . . \n",filename,text);
  35.  
  36. printf(">> %s found\n",filename);
  37. printf(">> Hunting for \%s\"\n",text);
  38.  
  39. /* Scan for a matching byte */
  40.  
  41. x = 1; //File offset
  42.  
  43. while(fgets(buffer,MAXCHAR,f))
  44. {
  45. b = buffer;
  46. if( (a = strstr(buffer,text)) != NULL)
  47. {
  48. matches++; //inc. match count
  49. printf("Found at offset %Lu\n",x+a-b);
  50. }
  51. x+=strlen(buffer); //adjust offset
  52. }
  53.  
  54. printf("\nFound a total of %Lu matches\n",matches);
  55. fclose(f);
  56. }

main.cpp program
  1. #include "search.cpp"
  2. #include "search.h"
  3.  
  4. void main()
  5. {
  6. dispfiles fileobj;
  7. char rootdir[MAXPATH];
  8.  
  9. rootdir[0] = '@' + 3;
  10. rootdir[1] = ':';
  11. rootdir[2] = 92;
  12. rootdir[3] = 0;
  13. setdisk(2);
  14. chdir(rootdir);
  15. strcpy(fileobj.allfiles,"*.exe");
  16. fileobj.showfiles();
  17. outfile.close();
  18. }
  19.  
  20.  
  21. char *dispfiles::getdirectory(char *path)
  22. {
  23. strcpy(path,"X:\\");
  24. path[0] = 'A' + getdisk();
  25. getcurdir(0,path+3);
  26. return(path);
  27. }
  28.  
  29. int dispfiles::showfiles(void)
  30. {
  31. attrib = 47;
  32. getdirectory(curdir);
  33. done = findfirst(allfiles,&fileinfo,attrib);
  34. if(!done) outfile << endl << "Path: " << curdir << endl;
  35. while(!done)
  36. {
  37. outfile << fileinfo.ff_name << endl;
  38. done = findnext(&fileinfo);
  39. }
  40. attrib = FA_DIREC;
  41. done = findfirst("*.*",&fileinfo,attrib);
  42. while(!done)
  43. {
  44. strncpy(temp,fileinfo.ff_name,2);
  45. if(((fileinfo.ff_attrib & FA_DIREC) == FA_DIREC) && (temp[0] != '.'))
  46. {
  47. if(strlen(curdir) !=3)strcat(curdir,"\\");
  48. strcat(curdir,fileinfo.ff_name);
  49. chdir(curdir);
  50. done = showfiles();
  51. chdir("..");
  52. getdirectory(curdir);
  53. }
  54. done = findnext(&fileinfo);
  55. }
  56. return done;
  57. }
Last edited by alc6379; Feb 1st, 2005 at 10:57 am. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Error measage in compiling c++ program

 
0
  #2
Feb 1st, 2005
you have two functions with the name 'main', one in each file. Only one function 'main' can exist in the program. Change one of them.

And, as Naru will tell you if I don't, it should be "int main()" not "void main()".
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: wildkms725 is an unknown quantity at this point 
Solved Threads: 0
wildkms725 wildkms725 is offline Offline
Newbie Poster

Re: Error measage in compiling c++ program

 
0
  #3
Feb 1st, 2005
Thank you for you help. Is there any way that I can combine the two .cpp files into one?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Error measage in compiling c++ program

 
0
  #4
Feb 2nd, 2005
No need, just rename one of the mains to be something else.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Error measage in compiling c++ program

 
0
  #5
Feb 2nd, 2005
Originally Posted by wildkms725
Thank you for you help. Is there any way that I can combine the two .cpp files into one?
Just combine them in your editor. Put all the header files near the top and sort them out for duplicates. Also obvious things like #include "search.cpp" are not needed any more. Occasionally the order of the header files might play a role.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC