【问题标题】:Redefinition errors of static variable in C++ [closed]C++中静态变量的重定义错误[关闭]
【发布时间】:2014-06-08 07:46:44
【问题描述】:

我在编译这些文件时遇到了重新定义错误。我已经查看了关于 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 

这是我的实现的实用程序.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: error: ‘std::ofstream Utility::c’ 之前在这里声明过

对于 Utility 类中的每个静态变量,依此类推。

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

【问题讨论】:

  • std 命名空间是...在您的 .cpp 文件中的确切位置?顺便说一句,您决定 粘贴的该错误消息的第二行将是 非常 告诉。 IE。 “这里”在哪里?
  • 我无法理解你的意思?
  • @WhozCraig 用确切的错误消息更新了问题
  • 谢谢。现在,utility.cpp 怎么知道 ostreamstd 命名空间中?您没有像在标题中那样在 .cpp 文件中完全指定命名空间是否有原因?您从utility.cpp 顶部定义您的类变量之前剪辑了10 行代码。有机会看到这些吗?
  • @WhozCraig 感谢您的回复。定义变量之前的那些行只是 cmets,而且我在 utility.cpp 中提到了 std。我只是忘了在我在这里发布的代码中提及它。更新了

标签: c++ linker header-files ld


【解决方案1】:

上面发布的标题缺少 endif。那肯定会导致您的错误。 那是转录错误吗?

【讨论】:

  • 实际上在实际代码文件中有一个#endif 和using namespace std,我只是错过了将它放在我发布的代码中。更正相同。
  • 你能告诉我们编译器吗?因为在Linux上使用gcc 4.9,只要我链接stdc++(-lstd++),并且写一个空的main,它就可以编译,并为我链接。有关 stdc++ 的更多信息,请参阅 (stackoverflow.com/questions/9447428/…)
  • 我在 ubuntu 12.04 64 位上使用 g++ 4.6.3。
【解决方案2】:

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

std::ofstream Utility::out;

【讨论】:

    猜你喜欢
    • 2018-12-10
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多