c++多定义链接错误与.h中声明的const
C++ multiple definition linking error with const declared in .h
我使用了一些在
中定义的常量Constanst.h
#ifndef __CONSTANTS_H__
#define __CONSTANTS_H__
namespace A{
namespace B{
const int FIRST = 1;
const int SECOND = 2;
}
}
#endif
我包括这个文件从2个不同的。cpp (a.p和b.p)
gcc版本3.2.3给了我"多重定义"链接错误
我解决这个问题的方法是添加另一个"File.h",其中包含" constants .h",并将相同的常量声明为"extern"
有人能解释一下吗?我以为这是C的问题,而不是c++
使用g++版本4.3.4:
constants.h:5: error: ISO C++ forbids declaration of `FIRST' with no type
添加类型int
后,正常运行。
所以也许你应该使用一个新的编译器?使用g++代替gcc?
相关文章:
- 为什么函数声明中允许 const?
- 在内存不足之前,我可以声明多少个 const 变量?
- 如何在声明为 const 的方法中更改类成员
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 可以使用<size_t>static_const(),std::as_const()或static_cast<const size_t>()来声明数组吗?
- 为什么必须将 const 添加到 constexpr 中才能进行字符串文字声明?
- 声明与 const 变量和成员函数相同的标识符
- 为什么我不能声明一个 constexpr 本地,而一个 const 一个工作?
- 类中的const声明
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- 为什么我们在这个函数声明中使用 const 和 & ?
- 为什么重载运算符>在参数声明 const 时不起作用?
- 在C 中声明const Pointer是常见的
- 声明“const”“boost::range”的正确方法
- 在声明'const'函数中循环访问具有'const_iterator'的集合会导致错误
- 什么是最好的以及如何在 C++ 函数中声明 const
- 我可以从引用传递的参数返回值中声明const int吗
- 在函数中声明const而不是变量有什么好处吗?
- 是否在类外部重新声明const静态变量
- 声明const全局变量时需要静态的