簡體   English   中英

使用g ++ -c的c ++編譯器錯誤:編譯器將const string&識別為int

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

我創建了一個具有構造函數的類Products

Product::Product(const int &num,const float &pr,const std::string &str):
number(num),price(pr),name(str)
{
}

當我使用g ++ -ci進行編譯時,我在標頭中有聲明,並且在另一個源文件中有一個實現,但出現錯誤:

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)

為什么會這樣呢? 為什么編譯器將const std :: string&替換為int?

問題是您忘記在標頭中#include <string> ,從而導致g ++發出可疑的診斷,而對於無法理解的類型,該診斷將依賴於int

我通過刪除同一文件夾中的一個名為Product.gch的文件解決了這個問題,我不知道它是如何解決的,但是它確實

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM