使用 G++ -C 的 C++ 编译器错误:编译器将 const 字符串识别为 int

c++ compiler error using g++ -c: compiler recognizes a const string& as int

本文关键字:编译器 const 字符串 识别 int 错误 G++ C++ 使用      更新时间:2023-10-16

>我创建了一个具有构造函数的产品类

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 的文件解决了这个问题我不知道它是如何解决的,但它确实解决了