Hello! Help me please. There is a Hero class -

class Hero { public: ... float wins; ... } 

There is a Result function

 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 an error - "type casting: it is impossible to convert" float "to" float * "... And the same focus with INT variables in the same class is channeled: (

  • Rewinding va_arg by class or structure is generally a very strange idea at least ... Well, and then: type va_arg(va_list ap, type); are you wrong? - PinkTux

2 answers 2

Use correctly!

  wins = va_arg(list, float); 

and everything will work out for you ... At least VC ++ understands everything perfectly and works ...

Update Regarding access to the fields ... Here is a real live compiled code for VC ++ 2015:

 #include <stdarg.h> #include <iostream> using namespace std; struct Test { float a,b,c; Test(float x):a(x),b(2*x),c(x*x){}; }; 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, you can transfer different objects.

  • Unfortunately, this is definitely not possible. access to values ​​inside the class is necessary, which, as explained below, alas, cannot be :( - Neuromanser
  • Do you want to transfer Hero objects there? The easiest way is to pass on their addresses, what's the problem? - Harry
  • See addition to answer. - Harry
  • Thank you very much! And the truth is working properly. Another small question ... Is it possible to refer to the same variable twice in the same cycle using va_arg? - Neuromanser
  • As far as I know, no. Each va_arg refers to the following argument. Save it in a variable, for example ... - Harry

It's not very clear what this code should do. But for starters:

 type va_arg(va_list ap, type); 

Passing Args.wins first argument Args.wins incorrect. Further, if this is corrected, then g ++ produces the following:

 main.cpp: In function 'int Result(float, Hero, ...)': main.cpp:14:29: warning: 'float' is promoted to 'double' when passed through '...' [enabled by default] wins = va_arg(list, float); ^ main.cpp:14:29: note: (so you should pass 'double' not 'float' to 'va_arg') main.cpp:14:29: note: if this code is reached, the program will abort 

But this is minor compared to the main mistake. It is impossible to transfer to processing va_* structure, not to mention the class. And that's why:

 struct Hero { char c; short s; double d; }; printf("%lu, %lu\n", sizeof(Hero), sizeof(char)+sizeof(short)+sizeof(double)); 

In my case, 16, 11 will be displayed, but not the fact that it will be so in yours :) va_* oriented on the size of the types, but in the case of structures, they cannot do this because of the alignment of the fields.

  • Thank you very much for the detailed response. The code is really unclear, cut out most, as not being of interest. Or maybe you can tell how you can access a random number of variables of the class type? - Neuromanser
  • Isn't it easier to pass an array or container to a function: float Sum(int count, const std::vector<Test> &arr) ? - Alexander