我需要從堆疊中創建一個類實體,但是根據一個變數,我需要使用不同的建構式來呼叫它
class A
{
public:
A(std::string str);
A(int value)
};
void main(void)
{
bool condition = true;
A class_a {condtion ? "123" : 456};
}
但我無法編譯它。
uj5u.com熱心網友回復:
三元運算子不能為trueand回傳不同的型別false。
你可以這樣解決:
A class_a = condition ? A("123") : A(456);
其他修復:
#include <string>
class A {
public:
A(std::string str) {} // the function must have an implementation
A(int value) {} // the function must have an implementation
};
int main() { // not void main
bool condition = true;
A class_a = condition ? A("123") : A(456);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368079.html
