0

I have been getting redefinition errors when I compile these files. I have looked through questions on SO regarding the same, but am unable to find where I am wrong.
Here are the files :-

     #ifndef UTILITY_H
     #define UTILITY_H

     //------------//
     //HEADER FILES//
   //------------//
#include <string>
#include <map> 
#include <fstream>  

//----------------------------//
/* Declaration of the class. */ 
// ---------------------------//  
class Utility
{
 public:
  static std::ofstream out ;
  static std::ofstream music ; 
  static std::ofstream video ; 
  static std::ofstream document ;
  static std::ofstream mp3 ; 
  static std::ofstream mp4 ; 
  static std::ofstream html ; 
  static std::ofstream deb ; 
  static std::ofstream cpp ;
  static std::ofstream c ;
  static std::ofstream py ; 
  static std::ofstream php ; 
  static std::ofstream java ; 
  static std::ofstream _class ;
  static std::ofstream so ;
  static std::ofstream master ; 
  static std::string lastPath ;
  static std::map<std::string, std::string> records ; 
  static bool openStream() ;  // to open the stream for indexing.
  static void index(std::string) ;   // to index.
  static bool closeStream() ; // to  close the streams after indexing.
  static void loadTree() ;  // to load the Tree at the start of the application.
  static void search(std::string, int) ; // to search for the required keyword. 
} ;  
#endif 

Here is the utility.cpp file in which I have my implementation :-

    #include "utility.h" 
    using namespace std ; 
/* The functions are in the temp.cpp file fo testing purposes. */ 

// -------------------------------------------------------//
/* Definition of the static variables outside the class. */ 
// ------------------------------------------------------//
ofstream Utility::out ;
ofstream Utility::music ; 
ofstream Utility::video ;
ofstream Utility::document ;     
string Utility::lastPath ; 
ofstream Utility::mp3 ; 
ofstream Utility::mp4 ; 
ofstream Utility::html ;
ofstream Utility::deb ; 
ofstream Utility::cpp ; 
ofstream Utility::c ;  
ofstream Utility::py ; 
ofstream Utility::php ;
ofstream Utility::java ; 
ofstream Utility::_class ; // because class is a keyword in c++. 
ofstream Utility::so ;
ofstream Utility::master ; 

The errors I am getting is :-

utility.cpp:18:19: error: redefinition of ‘std::ofstream Utility::c’ utility.h:72:10: error: ‘std::ofstream Utility::c’ previously declared here

and so on for every static variable in Utility class.

Can anybody please tell me what I am doing wrong here ?

Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
  • 3
    The `std` namespace is... where exactly in your .cpp file? The second line of that error message that you decided *not* to paste would be *extremely* telling, btw. I.e. where is "**here**" ? – WhozCraig Jun 08 '14 at 07:48
  • I am unable to understand what do you mean to say? – Pratik Singhal Jun 08 '14 at 07:50
  • @WhozCraig Updated the question with exact error message – Pratik Singhal Jun 08 '14 at 07:52
  • 1
    Thanks. Now, how does `utility.cpp` know that `ostream` is in the `std` namespace ? Is there a reason you didn't fully specify the namespace in your .cpp file as you did in your header? There are 10 lines of code you clipped out of the top of `utility.cpp` *before* your class vars are defined. Any chance of seeing those? – WhozCraig Jun 08 '14 at 07:53
  • @WhozCraig Thanks for the reply. Those lines before the variables were defined were just comments and also, I had mentioned `std` in `utility.cpp`. I just forgot to mention it in the code I posted here. Updated that – Pratik Singhal Jun 08 '14 at 07:59
  • This code does not give the error you posted (it indicates line 72 but you didn't post 72 lines). Please make sure that what you have posted is *exactly* the code that gives the error; nothing more and nothing less. – M.M Jun 08 '14 at 08:08
  • @MattMcNabb I am unable to understand this, I copied the same files to different directory and compiled, it compiled without errors. Then removed the already existing utility.o file existing within the original directory, it compiled succesfully in the original direcrtory as well! Can you please explain why this happened. – Pratik Singhal Jun 08 '14 at 08:16
  • Are you saying that your original code now seems to compile successfully? Are you using any IDE? – M.M Jun 08 '14 at 08:26
  • Yes! It is now compiling succesfully. I am not using any IDE working with a terminal and emacs – Pratik Singhal Jun 08 '14 at 12:14

2 Answers2

2

The posted header above is missing an endif. That could certainly cause your bug. Was that a transcription error?

Mickey Sweatt
  • 167
  • 2
  • 10
  • Actually there is a #endif as well as `using namespace std` in the actual code file, I just missed to put it here in the code which I have posted. Correcting the same. – Pratik Singhal Jun 08 '14 at 07:55
  • Could you tell us the compiler. Because using gcc 4.9 on Linux, as long as I link stdc++ (-lstd++), and write a empty main It compiles, and links for me. See (http://stackoverflow.com/questions/9447428/very-simple-program-not-working-in-c) for more info on stdc++ – Mickey Sweatt Jun 08 '14 at 08:04
  • I am using g++ 4.6.3 on ubuntu 12.04 64 bit. – Pratik Singhal Jun 08 '14 at 08:05
1

Use explicit namespace while defining all your variables in your source file:

std::ofstream Utility::out;
cppcoder
  • 22,227
  • 6
  • 56
  • 81