Dear love Community,
excause me for this little stupid Question.
The Situation:
I have a File x.h:
typedef std::pair<float, float> Bund;
I have new a y.h:
#include "Data.h"
class A
{
public:
Bund n;
and new in Data.cpp:
int x1 = SpinDurationMin->GetValue();
int x2 = SpinDurationMax->GetValue();
Bund m (float(x1), float(x2));
n = m;
I get a Error of Type mismatch:
template argument deduction/substitution failed:
TrackParameter.cpp:70:17: note: mismatched types ‘std::pair<_T1, _T2>’ and ‘Bund(float, float)’ {aka ‘std::pair<float, float>(float, float)’}
#include <iostream>
#include <utility>
using Bund = std::pair<float, float> ;
class A {
public:
Bund n;
};
int main()
{
int x1 = 1;
int x2 = 2;
Bund m (float(x1), float(x2));
A a;
a.n = m;
std::cout << "first: " << a.n.first << "; second: " << a.n.second << '\n';
}
Output:
error: no match for 'operator='
(operand types are 'Bund' {aka 'std::pair<float, float>'}
and 'Bund(float, float)' {aka 'std::pair<float, float>(float, float)'})
20 | a.n = m;
| ^
It seems it can’t understand (float(x1), float(x2)) is an attempt to convert int to float…
#include <iostream>
#include <utility>
using Bund = std::pair<float, float> ;
class A {
public:
Bund n;
};
int main()
{
int x1 = 1;
int x2 = 2;
Bund m { float(x1), float(x2) };
A a;
a.n = m;
std::cout << "first: " << a.n.first << "; second: " << a.n.second << '\n';
}
Output:
first: 1; second: 2
I’d go with the explicit cast (static_cast<>) AND the uniform initializer. IMHO.
But we need to wait for some syntax guru: there’s quite a few of them on this forum.
The general syntax parsing rule here is that if something can be a function declaration, it is a function declaration.
To the compiler, Bund m (float(x1), float(x2));
means "Declare a function named m that takes in two floats (parameters x1, x2) and returns a Bund".
The parentheses around (x1) and (x2) are then seen as unnecessary, but still allowed.