"multiple definition of" 错误
来源:互联网 发布:厦门大学软件学院分数 编辑:程序博客网 时间:2024/04/29 05:47
"multiple definition of" 错误
在global.h定义了一个常量字符串,在多个cpp中包含该global.h.
// file: global.h
#ifndef GLOBAL_H
#define GLOBAL_H
const char * STR_TEST = "Hello world!";
#endif
结果链接时出现错误:
multiple definition of ‘STR_TEST’
虽然改为#define肯定行,但是尽量应该用const量取代宏定义。
这里有个概念性错误。
const 变量默认是 static 的,
但带有 const 的不一定是 const 变量.
此处 STR_TEST 并不是常量,而是指向常量字符串的变量。
改为
const char * const STR_TEST = "Hello world!";
就可以了。
不过我又觉得改为
const char STR_TEST[] = "Hello world!";
更好。
在global.h定义了一个常量字符串,在多个cpp中包含该global.h.
// file: global.h
#ifndef GLOBAL_H
#define GLOBAL_H
const char * STR_TEST = "Hello world!";
#endif
结果链接时出现错误:
multiple definition of ‘STR_TEST’
虽然改为#define肯定行,但是尽量应该用const量取代宏定义。
这里有个概念性错误。
const 变量默认是 static 的,
但带有 const 的不一定是 const 变量.
此处 STR_TEST 并不是常量,而是指向常量字符串的变量。
改为
const char * const STR_TEST = "Hello world!";
就可以了。
不过我又觉得改为
const char STR_TEST[] = "Hello world!";
更好。
- "multiple definition of" 错误
- multiple definition of错误原因
- multiple definition of 错误原因
- gcc交叉编译错误: multiple definition of
- QT 编译错误 multiple definition of
- 编译报multiple definition of******错误
- multiple definition of错误的解决
- C编译错误--multiple definition of 'XXX'
- C++ 解决 multiple definition of & previous definition here编译错误
- multiple definition of...
- multiple definition of...
- 解决 multiple definition of
- extern: multiple definition of
- gcc: multiple definition of
- multiple definition of XXXX
- 解决 multiple definition of
- multiple definition of
- 116、multiple definition of
- 刚开通的博客
- 简单的css
- 自己动手写键盘测试软件
- Chap1 Milking Cows
- js - 图标在网页上漂浮移动效果
- "multiple definition of" 错误
- 由系统时间反求UTC(格林尼治时间)
- IE浏览器->属性->高级里的"禁止脚本调试"的设置在注册表里的什么位置
- char *const p;与 const char * p;的区别
- 快受不了MYSQL了,简直要疯了
- 《Javascript高级程序设计》读书笔记——ECMAScript基础(2)
- 原创 编程如禅——工厂模式的智能化实现(c++)(1)引子
- 如何限制Internet Explorer选项
- Windows多线程之信号量(Semaphore) {不断补充中...最后更新08.08.26}