使用 G++ -C 的 C++ 编译器错误:编译器将 const 字符串识别为 int
c++ compiler error using g++ -c: compiler recognizes a const string& as int
>我创建了一个具有构造函数的产品类
Product::Product(const int &num,const float &pr,const std::string &str):
number(num),price(pr),name(str)
{
}
我在标头中有声明,在另一个源文件中有一个实现当我使用 g++ -c 编译它时,我收到一个错误:
g++ -c Product.cpp
Product.cpp:14: error: prototype for 'Product::Product(const int&, const float&, const std::string&)' does not match any in class 'Product'
Product.h:5: error: candidates are: Product::Product(const Product&)
Product.h:10: error: Product::Product(const int&, const float&, int)
为什么会这样?为什么编译器用 int 替换 const std::string & ?
问题是你忘记在标头中#include <string>
,导致 g++ 发出一个有问题的诊断,该诊断回退到它不理解的类型int
。
我通过删除同一文件夹中名为 Product.gch 的文件解决了这个问题我不知道它是如何解决的,但它确实解决了
相关文章:
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 为什么我的编译器在调用 const getter 函数时抛出"转换丢失(const)限定符"错误?
- 如何在构造函数中传递 const 引用时强制编译器不接受右值
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 编译器在 const ref 类型参数上使用临时对象时是否应该警告不安全的行为?
- 是否不允许编译器假设 const-ref 参数将保持 const?
- C 编译器可以执行用于返回值的命名const变量的RVO
- 将 const 限定符应用于模板参数时的不同编译器行为
- 编译器忽略函数参数上的'const'
- 编译器可以静默地从 const 转换为 const 和,反之亦然吗?
- C++17:是编译器为(静态存储持续时间)const引用绑定创建的可修改的临时对象(和存储)
- 编译器"error: passing ‘const something’ as ‘this’ argument discards qualifiers"
- 编译器不会为 "const" 和"not-const"函数给出不明确的错误吗
- 使用工厂方法时编译器错误:无法转换"const std::p air<char* const
- C :使用Visual Studio编译器时const Pointers
- 编译器如何知道是否调用 const 重载
- 如何修复C++编译器错误"cannot convert 'Type' to 'const Type*'"?
- 如何在没有编译器警告的情况下返回对空字符串的 const 引用?
- 使编译器假定 const primairy 类型作为类
- Intel c++编译器- const string是可修改的