C+++. Using "va_arg" a change-of-type error (possible to transform float into float)
-
Hey! Please help me. There's Hero--
class Hero { public: ... float wins; ... }
It's Result.
int Result(float draw, Hero Args, ...) { int i = 0; float wins; va_list list; va_start(list, Args); while (i != 2) { wins = va_arg(Args.wins, float); cout << wins << endl; i++; } va_end(list); return 0; }
When compiling, I get a mistake, "Impossible to transform "float" into "float." But the same trick with INT variables in the same class is :
-
Use it right!
wins = va_arg(list, float);
And you'll be able to... At least VC+++ is very clear and working. ♪ ♪
UpdateOn the treatment of the fields... This is a real live code for VC++ 2015:
#include <stdarg.h> #include <iostream>
using namespace std;
struct Test
{
float a,b,c;
Test(float x):a(x),b(2x),c(xx){};
};float Sum(int count, Test Args, ...)
{
float sum = Args.a;
va_list list;
va_start(list, Args);
for(int i = 0; i < count-1; ++i)
{
sum += va_arg(list, Test).a;
}
va_end(list);
return sum;
}int main()
{
cout << Sum(3,Test(2.0),Test(4.0),Test(5.0)) << endl;
}
As you can see, different objects can be transferred.