c++中静态变量重定义错误
Redefinition errors of static variable in C++
当我编译这些文件时,我一直得到重新定义错误。我已经看过关于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: error: ' std::ofstream Utility::c '先前声明在这里
等等,用于Utility类中的每个静态变量。
谁能告诉我我在这里做错了什么?上面发布的标题缺少一个结束符。这肯定会引起你的疾病。这是抄写错误吗?
在源文件中定义所有变量时使用显式命名空间:
std::ofstream Utility::out;
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件