C ++中静态变量的重定义错误

时间:2014-06-08 07:46:44

标签: c++ linker header-files ld

编译这些文件时,我一直在重新定义错误。我已经在SO上查询了相同的问题,但我无法找到我错的地方 以下是文件: -

     #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 

这是我的实现中的utility.cpp文件: -

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

我得到的错误是: -

  

utility.cpp:18:19:错误:重新定义'std :: ofstream Utility :: c'   utility.h:72:10:错误:'std :: ofstream Utility :: c'先前在这里声明

对于Utility类中的每个静态变量,

等等。

有谁能告诉我这里做错了什么?

2 个答案:

答案 0 :(得分:1)

上面发布的标题缺少一个endif。这肯定会导致你的错误。 这是转录错误吗?

答案 1 :(得分:1)

在源文件中定义所有变量时使用显式命名空间:

std::ofstream Utility::out;