【发布时间】: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怎么知道ostream在std命名空间中?您没有像在标题中那样在 .cpp 文件中完全指定命名空间是否有原因?您从utility.cpp顶部在定义您的类变量之前剪辑了10 行代码。有机会看到这些吗? -
@WhozCraig 感谢您的回复。定义变量之前的那些行只是 cmets,而且我在
utility.cpp中提到了std。我只是忘了在我在这里发布的代码中提及它。更新了
标签: c++ linker header-files ld