c ++重载函数,包括布尔和整数参数
c++ overloading function including bool and integer arguments
一个简单且我想很容易回答的问题(如果我自己还没有回答的话)。以下重载函数:
void BR(const bool set) { backwardReaction_[nReac_] = set; }
bool BR(const int reactionNumber) const { return backwardReaction_[reactionNumber]; }
第一个函数是 setter,第二个函数是 getter。 backwardReaction_
属于 std::vector<bool>
类型。每当我想调用第二个函数时就会出现问题。这里我得到一个编译器错误 overload function BR(xy) ambigious
.
int main()
.
.
const int i = 3;
bool a = chem.BR(i);
编译器错误等于:
chemistryTestProg.cpp: In function ‘int main()’:
chemistryTestProg.cpp:74:34: error: call of overloaded ‘BR(const int&)’ is ambiguous
const bool a = chem.BR(i);
^
In file included from ../../src/gcc/lnInclude/chemistryCalc.hpp:38:0,
from ../../src/gcc/lnInclude/chemistry.hpp:38,
from chemistryTestProg.cpp:35:
../../src/gcc/lnInclude/chemistryData.hpp:266:18: note: candidate: void AFC::ChemistryData::BR(bool)
void BR(const bool);
^~
../../src/gcc/lnInclude/chemistryData.hpp:322:22: note: candidate: bool AFC::ChemistryData::BR(int) const
bool BR(const int) const;
^~
我想我遇到问题是因为类型 bool
和 int
是相同的 (true => int(1), false => int(0)
。当我将 getter 名称更改为,例如,bool getBR(const int reactionNumber) {...}
一切正常。所以我猜问题出在 c++ 中 bool
和 int
处理的相似性。我还尝试了各种不同的调用,例如:
const bool a = chem.BR(4)
const bool a = chem.BR(int(5))
const bool a = chem.BR(static_cast<const int>(2))
bool a = chem.BR(...)
因此,我认为这确实与 bool
和 int
重载参数有关。尽管如此,我快速搜索了一下,并没有找到太多关于这两种重载类型和由此产生的问题。托比
这是因为您声明 BR(int)
而不是 BR(bool)
为 const
。然后当你在一个非常量对象上调用 BR(int)
时,编译器有两个相互冲突的匹配规则:参数匹配有利于 BR(int)
,但 const
-ness 匹配有利于 BR(bool)
.
一个简单且我想很容易回答的问题(如果我自己还没有回答的话)。以下重载函数:
void BR(const bool set) { backwardReaction_[nReac_] = set; }
bool BR(const int reactionNumber) const { return backwardReaction_[reactionNumber]; }
第一个函数是 setter,第二个函数是 getter。 backwardReaction_
属于 std::vector<bool>
类型。每当我想调用第二个函数时就会出现问题。这里我得到一个编译器错误 overload function BR(xy) ambigious
.
int main()
.
.
const int i = 3;
bool a = chem.BR(i);
编译器错误等于:
chemistryTestProg.cpp: In function ‘int main()’:
chemistryTestProg.cpp:74:34: error: call of overloaded ‘BR(const int&)’ is ambiguous
const bool a = chem.BR(i);
^
In file included from ../../src/gcc/lnInclude/chemistryCalc.hpp:38:0,
from ../../src/gcc/lnInclude/chemistry.hpp:38,
from chemistryTestProg.cpp:35:
../../src/gcc/lnInclude/chemistryData.hpp:266:18: note: candidate: void AFC::ChemistryData::BR(bool)
void BR(const bool);
^~
../../src/gcc/lnInclude/chemistryData.hpp:322:22: note: candidate: bool AFC::ChemistryData::BR(int) const
bool BR(const int) const;
^~
我想我遇到问题是因为类型 bool
和 int
是相同的 (true => int(1), false => int(0)
。当我将 getter 名称更改为,例如,bool getBR(const int reactionNumber) {...}
一切正常。所以我猜问题出在 c++ 中 bool
和 int
处理的相似性。我还尝试了各种不同的调用,例如:
const bool a = chem.BR(4)
const bool a = chem.BR(int(5))
const bool a = chem.BR(static_cast<const int>(2))
bool a = chem.BR(...)
因此,我认为这确实与 bool
和 int
重载参数有关。尽管如此,我快速搜索了一下,并没有找到太多关于这两种重载类型和由此产生的问题。托比
这是因为您声明 BR(int)
而不是 BR(bool)
为 const
。然后当你在一个非常量对象上调用 BR(int)
时,编译器有两个相互冲突的匹配规则:参数匹配有利于 BR(int)
,但 const
-ness 匹配有利于 BR(bool)
.