尝试创建流的错误日志 - 获取"one or more multiply defined symbols found"
Trying to create an error log ofstream -- getting "one or more multiply defined symbols found"
我正在尝试这样做:
#pragma once
#include <fstream>
#include <string>
static std::ofstream ErrorLog;
void InitErrorLog(std::string FileName) {
ErrorLog.open(FileName);
}
但是在多个 CPP 文件中 #include 时收到"找到一个或多个乘法定义的符号"错误。STL 在做什么(提供 cout、cin、cerr 等——这种方法起源于重定向 cerr 的替代方案)而我不是?
您将在头文件中提供ErrorLog
的定义。相反,在源文件中定义它,并在标头中保留一个 extern 声明。
源
std::ofstream ErrorLog;
void InitErrorLog(std::string FileName) {
ErrorLog.open(FileName);
}
页眉
extern std::ofstream ErrorLog;
void InitErrorLog(std::string FileName);
此外,为了将函数保持在标头处,您必须使其inline
。
你打破了一个定义规则。您需要使该方法inline
.
inline void InitErrorLog(std::string FileName) {
ErrorLog.open(FileName);
}
另外,请注意,通过声明变量 static
,您将拥有每个翻译单元的副本 - 即它不是全局的。要使其全局化,您需要在标头中extern
声明它,并在单个实现文件中定义它。
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- 模式"allocate memory or use existing data"
- N-API include an .so or dll
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 将 OR 逻辑运算符从 C++ 转换为 Fortran
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 我如何将 OR 与 if 语句一起使用字符串
- 将 if 语句中的字符与 or 进行比较
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- "No such file or directory" C++标头问题
- 处理 std::enable_if<...中谓词的逻辑"OR">
- C++ 虽然语句不适用于字符串和"or"
- 编译时"one or more multiply defined symbols found"错误
- 尝试创建流的错误日志 - 获取"one or more multiply defined symbols found"
- 在基本游戏中LNK1169 "one or more multiply defined symbols found"
- C++14/1y: "operator+ must take either one or two arguments"的标准参考 ?