Struct

Can someone pls explain me why this wont work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17




  struct S {
    int i;
    double d;
    const char * s;
};

int main() {
    S s1 = { 3, 47.9, "string one" };

    printf("s1: %d, %f, %s\n", s1.i, s1.d, s1.s);

    return 0;
}


Error list:

Error C2628 'S' followed by 'int' is illegal (did you forget a ';'?) line 7
Error C2079 'i' uses undefined struct 'S' line 7
Error C2059 syntax error: '}' line 10
Error C2143 syntax error: missing ';' before '}' line 10
Error C2079 's1' uses undefined struct 'S' line 14
Error C2440 'initializing': cannot convert from 'initializer list' to 'int' line 14
Error C2079 's2' uses undefined struct 'S' line 15
Error C2440 'initializing': cannot convert from 'initializer list' to 'int' line 15
Warning C4473 'printf' : not enough arguments passed for format string line 18
Error C2143 syntax error: missing ')' before ';' line 18

Warning C4477 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 1 has type 'int'
Warning C4313 'printf': '%s' in format string conflicts with argument 1 of type 'int'
Warning C4473 'printf' : not enough arguments passed for format string
Last edited on
Did you include the correct header?
 
#include <cstdio> 

It compiled for me with no errors.
Yes i included the correct header
You did not post what you tried to compile.
Please post EXACTLY what you tried to compile.
or, maybe, what compiler this is. I don't track all the versions well but that assignment statment isnt legal in old c++, is it? Make sure you are using c++ 17?
Last edited on
that assignment statment isnt legal in old c++, is it?

The assignment is legal even in C, so it's certainly legal in old C++.
In C it would require "struct S" instead of just S.
struct S s1 = { 3, 47.9, "string one" };

But you'd get a completely different error message, w/o the "struct",
main.c:11:5: error: unknown type name ‘S’; use ‘struct’ keyword to refer to the type
   11 |     S s1 = { 3, 47.9, "string one" };
      |     ^
      |     struct 

(But maybe only recent compilers give clear error messages)
Last edited on
Hey, wait a second. OP's error messages don't match up with what is shown. There isn't even an 's2' variable.
The compiler being used is C++, so my previous message is moot (you can tell because it uses terminology like "initializer list").

Hajo93, you are wasting people's time by showing your interpretation of the code instead of the actual code.

Last edited on
It would be helpful if the OP posted a Short, Self Contained, Correct (Compilable), Example instead of the uncompileable snippet we got.

http://www.sscce.org/

If that has been done then determining if all the proper headers had been included would be easy. As well as having the error messages match the code.

Why use C's printf in C++ code? C++20 now has std::format, though not all current compilers have the capability at this time*.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <format>   // https://en.cppreference.com/w/cpp/utility/format

// for a list of basic formatting options: 
// https://docs.python.org/3/library/string.html#formatspec

struct S
{
   int i;
   double d;
   const char* s;
};

int main()
{
   S s1 = { 3, 47.9, "string one" };

   std::cout << std::format("s1: {}, {}, {}\n", s1.i, s1.d, s1.s);
}
s1: 3, 47.9, string one

*Visual Studio is the only current compiler I know of that is fully C++20 compliant at this time, all others are missing features.
https://en.cppreference.com/w/cpp/20

There is a workaround if one's compiler isn't 100% compliant, the fmt 3rd-party library: https://fmt.dev/latest/index.html

One thing to note about MSVC++, there are parts of C++20 that require compiling against std::c++latest, not std:c++20. Otherwise MSVC++ goes all whingey.
Topic archived. No new replies allowed.