Scene:
The problem is that the constructor for is missing or is declared as explicit.
Please refer to the following scenario.
#include <iostream>
using std::cout;
using std::endl;
class Rational1
{
public:
Rational1(int n = 0, int d = 1):num(n), den(d)
{
cout << __func__ << "(" << num << "/" << den << ")" << endl;
}
public:
int num;
int den;
};
class Rational2
{
public:
explicit Rational2(int n = 0, int d = 1) :num(n), den(d)
{
cout << __func__ << "(" << num << "/" << den << ")" << endl;
}
public:
int num;
int den;
};
void Display1(Rational1 r)
{
cout << __func__ << endl;
}
void Display2(Rational2 r)
{
cout << __func__ << endl;
}
int main()
{
Rational1 r1 = 11;
Rational1 r2(11);
Rational2 r3 = 11; // error E0415
Rational2 r4(11);
Display1(1);
Display2(2); // error E0415
return 0;
}
Explicit keyword
1. Specifies that the constructor or conversion function (from C++11) is explicit, that is, it cannot be used for implicit conversion and copy initialization
2. Explicit can be used with constant expressions The function is explicit if and only if the constant expression evaluates to true (From C++20)
Problem description
Error:E0415 no suitable constructor exists to convert from “int“ to “Rational“
Solution:
1. Implement the corresponding constructor yourself. (Recommended)
2. Delete the constructor modified by the explicit keyword. (Not recommended)
Read More:
- [Solved] Error: The superclass, ‘Animal‘, has no unnamed constructor that takes no arguments.
- [Solved] runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator<int>>‘
- [Solved] NTP Sync common error: no server suitable for synchrnization found
- How to Solve Error: Type mismatch: cannot convert from Object to Car
- ORA-08103: object no longer exists [How to Solve]
- Yolox_s.pth Convert to tensorRT Error: AttributeError: ‘tensorrt.tensorrt.Builder‘ object has no attribute ‘max_workspace_size‘
- SpringBoot startup error Failed to determine a suitable driver class
- [Solved] Bringing up interface eth0: Error: No suitable device found: no device found for connection ‘System eth0’.
- How to Fix Cannot convert value of type Error
- How to Solve Springboot Error: Failed to convert value of type
- QSP License error: Submit failed! Message: ERROR -Locator 304511e2e493 is already associated to another license
- SQL Server Error: Arithmetic overflow error converting expression to data type int.
- [Solved] Milvus Error: [ERROR][SERVER][TakeToEUse milvus.has_collection to verify whether the collection exists
- [Solved] Click the play button to display: e/mediaplayer: error (1, – 2147483648)
- [Solved] Error in inherits(x, “theme”): argument “e2” is missing, with no default
- error: remote origin already exists. [How to Solve]
- Clickhouse error: XXXX.XXXX_local20211009 (8fdb18e9-bb4c-42d8-8fdb-18e9bb4c02d8): auto…
- [Solved] Doris Error: Label Already Exists
- error LNK2019: Unresolvable external symbols “__declspec(dllimport) public: int __thiscall QString::toWCharArray(wchar_t
- error: remote origin already exists [How to Solve]