Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. error C2059

error C2059

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comlinuxdata-structures
31 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mo1492

    This is a combination of all of the above responses. I have compiled this in mfc and it works. struct data { int something; }; struct container { int something_before; struct data data_item; int something_after; }; #define list_entry(ptr, type, member) \ ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member))) // <-- here is the error ... struct data *data_ptr; struct container *cont_ptr = list_entry(data_ptr, struct container, data_item); // This is the code you say is not compiling. #define list_next_entry(pos, member) list_entry(0, 0, 0) // This error is caused by the above line because list_entry(0,0,0) '0' is not a valid data type. const int nTest = list_next_entry(0, 0); // <-- error C2059: syntax error : ')' // If you do this instead... #define list_next_entry(pos, member) list_entry(data_ptr, struct container, data_item) // You will get a new error you will have to resolve in some way. // error C2440: 'initializing' : cannot convert from 'container *' to 'const int' // 1> There is no context in which this conversion is possible // The compiler can't convert a pointer to an int. // Don't know what you are trying to do. const int nTest = list_next_entry(0, 0);

    _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #14

    I am just trying to integrate a C old code into MFC project. I will try to do what you said, and give you a feedback.

    1 Reply Last reply
    0
    • M mo1492

      This is a combination of all of the above responses. I have compiled this in mfc and it works. struct data { int something; }; struct container { int something_before; struct data data_item; int something_after; }; #define list_entry(ptr, type, member) \ ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member))) // <-- here is the error ... struct data *data_ptr; struct container *cont_ptr = list_entry(data_ptr, struct container, data_item); // This is the code you say is not compiling. #define list_next_entry(pos, member) list_entry(0, 0, 0) // This error is caused by the above line because list_entry(0,0,0) '0' is not a valid data type. const int nTest = list_next_entry(0, 0); // <-- error C2059: syntax error : ')' // If you do this instead... #define list_next_entry(pos, member) list_entry(data_ptr, struct container, data_item) // You will get a new error you will have to resolve in some way. // error C2440: 'initializing' : cannot convert from 'container *' to 'const int' // 1> There is no context in which this conversion is possible // The compiler can't convert a pointer to an int. // Don't know what you are trying to do. const int nTest = list_next_entry(0, 0);

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #15

      The original code is just like that:

      #define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member)

      but the errors is little more then:

      error C2100: illegal indirection
      error C2059: syntax error : ')'
      error C2059: syntax error : 'bad suffix on number'
      error C3861: 'typeof': identifier not found

      all of them is in the same line ...

      S 1 Reply Last reply
      0
      • _ _Flaviu

        The original code is just like that:

        #define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member)

        but the errors is little more then:

        error C2100: illegal indirection
        error C2059: syntax error : ')'
        error C2059: syntax error : 'bad suffix on number'
        error C3861: 'typeof': identifier not found

        all of them is in the same line ...

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #16

        You can't use -> or * on a value of 0! That causes the compiler errors. Try any non-null value, and at least these compiler errors should go away. Moreover, typeof is not standard C/C++. There are implementations for that in the GCC extension for C, or in the BOOST library. You may need to find the correct BOOST library and include that in your project to make this code work.

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

        _ 3 Replies Last reply
        0
        • S Stefan_Lang

          You can't use -> or * on a value of 0! That causes the compiler errors. Try any non-null value, and at least these compiler errors should go away. Moreover, typeof is not standard C/C++. There are implementations for that in the GCC extension for C, or in the BOOST library. You may need to find the correct BOOST library and include that in your project to make this code work.

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #17

          Should I understand that pos has 0 value ? And what I could use instead of

          typeof

          without using BOOST ?

          1 Reply Last reply
          0
          • S Stefan_Lang

            You can't use -> or * on a value of 0! That causes the compiler errors. Try any non-null value, and at least these compiler errors should go away. Moreover, typeof is not standard C/C++. There are implementations for that in the GCC extension for C, or in the BOOST library. You may need to find the correct BOOST library and include that in your project to make this code work.

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #18

            This is the original code:

            #define list_next_entry(pos, member) \
            list_entry((pos)->member.next, typeof(*(pos)), member)

            1 Reply Last reply
            0
            • S Stefan_Lang

              You can't use -> or * on a value of 0! That causes the compiler errors. Try any non-null value, and at least these compiler errors should go away. Moreover, typeof is not standard C/C++. There are implementations for that in the GCC extension for C, or in the BOOST library. You may need to find the correct BOOST library and include that in your project to make this code work.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #19

              So, as a conclusion, the code is:

              const int nTest = list\_next\_entry(0, 0);
              

              where list_next_entry is defined:

              #define list_next_entry(pos, member) \
              list_entry((pos)->member.next, typeof(*(pos)), member)

              and list_entry is defined as:

              #define list_entry(ptr, type, member) \
              ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))

              and for this code I get:

              error C2100: illegal indirection
              error C2059: syntax error : ')'
              error C2059: syntax error : 'bad suffix on number'
              error C3861: 'typeof': identifier not found

              at line "const int nTest = list_next_entry(0, 0);" I cannot get rid of these errors ... :(

              L 1 Reply Last reply
              0
              • _ _Flaviu

                So, as a conclusion, the code is:

                const int nTest = list\_next\_entry(0, 0);
                

                where list_next_entry is defined:

                #define list_next_entry(pos, member) \
                list_entry((pos)->member.next, typeof(*(pos)), member)

                and list_entry is defined as:

                #define list_entry(ptr, type, member) \
                ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))

                and for this code I get:

                error C2100: illegal indirection
                error C2059: syntax error : ')'
                error C2059: syntax error : 'bad suffix on number'
                error C3861: 'typeof': identifier not found

                at line "const int nTest = list_next_entry(0, 0);" I cannot get rid of these errors ... :(

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #20

                _Flaviu wrote:

                I cannot get rid of these errors

                And you will not get rid of them as long as you keep using 0 as the parameters to the list_next_entry call. The define uses specific C/C++ types that are not valid if replaced by 0. In the above case this code translates as follows:

                // your statement
                const int nTest = list_next_entry(0, 0);

                // converts to:
                list_entry((0)->0.next, typeof(*(0)), 0)

                which makes no sense at all.

                _ 1 Reply Last reply
                0
                • L Lost User

                  _Flaviu wrote:

                  I cannot get rid of these errors

                  And you will not get rid of them as long as you keep using 0 as the parameters to the list_next_entry call. The define uses specific C/C++ types that are not valid if replaced by 0. In the above case this code translates as follows:

                  // your statement
                  const int nTest = list_next_entry(0, 0);

                  // converts to:
                  list_entry((0)->0.next, typeof(*(0)), 0)

                  which makes no sense at all.

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #21

                  Here is the code:

                  const file\_info\* file = list\_first\_entry(&dir\_list->list, const file\_info, list);
                  const file\_info\* file2 = td\_list\_next\_entry(file, list);   // <-- errors
                  

                  and I get the same errors:

                  error C2059: syntax error : ')'
                  error C3861: 'typeof': identifier not found

                  V S 3 Replies Last reply
                  0
                  • _ _Flaviu

                    Here is the code:

                    const file\_info\* file = list\_first\_entry(&dir\_list->list, const file\_info, list);
                    const file\_info\* file2 = td\_list\_next\_entry(file, list);   // <-- errors
                    

                    and I get the same errors:

                    error C2059: syntax error : ')'
                    error C3861: 'typeof': identifier not found

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #22

                    _Flaviu wrote:

                    and I get the same errors:

                    error C2059: syntax error : ')'
                    error C3861: 'typeof': identifier not found

                    Of course you must get these error messages! Just because

                    Quote:

                    'typeof'

                    does not exist in MFC (nor in Microsoft C++)

                    1 Reply Last reply
                    0
                    • _ _Flaviu

                      Here is the code:

                      const file\_info\* file = list\_first\_entry(&dir\_list->list, const file\_info, list);
                      const file\_info\* file2 = td\_list\_next\_entry(file, list);   // <-- errors
                      

                      and I get the same errors:

                      error C2059: syntax error : ')'
                      error C3861: 'typeof': identifier not found

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #23

                      Did you try to use [decltype (C++) | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/cpp/decltype-cpp?view=vs-2019) rather than typeof?

                      _ 1 Reply Last reply
                      0
                      • V Victor Nijegorodov

                        Did you try to use [decltype (C++) | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/cpp/decltype-cpp?view=vs-2019) rather than typeof?

                        _ Offline
                        _ Offline
                        _Flaviu
                        wrote on last edited by
                        #24

                        I have tried:

                        #define list_next_entry(pos, member) \
                        list_entry((pos)->member.next, decltype(*(pos)), member)

                        and I get another errors:

                        error C2528: 'abstract declarator' : pointer to reference is illegal
                        error C2528: 'abstract declarator' : pointer to reference is illegal
                        error C2227: left of '->list' must point to class/struct/union/generic type

                        seem to go for solving :) ... I guess ...

                        L 1 Reply Last reply
                        0
                        • _ _Flaviu

                          I am struggling from some time to an error:

                          error C2059: syntax error : ')'

                          I have somewhere in the old code:

                          #define list_entry(ptr, type, member) \
                          ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member))) // <-- here is the error ...

                          I have tried this solution, without success: c - list_entry in Linux - Stack Overflow[^] How can I get rid of this error ? I am trying to integrate this code in an MFC project, and the code from above is from C code (for linux I guess)

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #25

                          After referring to the SO link you helpfully provided, I finally understand what this macro is intended for. The solution is simple: don't use this macro! It is intended for an extremely specific purpose with specific classes/structs, and it must be used with very specific arguments. If you don't know how to use it correctly, the preprocessor will generate garbagage code, and the compiler unintellegible errors! The purpose is some low level memory address juggling which makes a whole lot of assumptions on behalf of the parameters being passed, without giving the compiler any information about what is going on. This is highly explosive stuff! Obviously you have no idea what parameters to pass and how to use it, probably not even what to use it for. Please don't do that and stop before anyone gets hurt. The only thing that's worse than using #define macros in C++ is using other peoples #define macros without being 200% sure what it does and how it's supposed to be used. If you need to ask what it does or why it doesn't work, then just drop it. Instead, just tell us what goal you intend to achieve, so we can advise you on proper C++ ways of solving your problem.

                          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                          1 Reply Last reply
                          0
                          • _ _Flaviu

                            I have tried:

                            #define list_next_entry(pos, member) \
                            list_entry((pos)->member.next, decltype(*(pos)), member)

                            and I get another errors:

                            error C2528: 'abstract declarator' : pointer to reference is illegal
                            error C2528: 'abstract declarator' : pointer to reference is illegal
                            error C2227: left of '->list' must point to class/struct/union/generic type

                            seem to go for solving :) ... I guess ...

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #26

                            What do you think decltype is going to do for you?

                            _ 1 Reply Last reply
                            0
                            • _ _Flaviu

                              Here is the code:

                              const file\_info\* file = list\_first\_entry(&dir\_list->list, const file\_info, list);
                              const file\_info\* file2 = td\_list\_next\_entry(file, list);   // <-- errors
                              

                              and I get the same errors:

                              error C2059: syntax error : ')'
                              error C3861: 'typeof': identifier not found

                              S Offline
                              S Offline
                              Stefan_Lang
                              wrote on last edited by
                              #27

                              Took me a while to figure it out, but decltype(*pos) will give you a reference type, and the parts of the macro using this type to cast to type* then fail, because you can't create a pointer to a reference! However, that's where std::remove_reference comes to the rescue! Try these definitions:

                              #include // for std::remove_reference

                              using namespace std;

                              #define list_entry(ptr, type, member) \
                              ((type *)((char *)(ptr)-(size_t)(&((type *)nullptr)->member)))
                              #define list_next_entry(pos, member) \
                              list_entry((pos)->member.next, remove_reference< decltype(*(pos)) >::type, member)

                              P.S.: are you sure the second #define you posted is correct? I could make up some type definitions that I could use with these macros without encountering compiler errors, but I'm not at all sure the macros would provide a valid pointer for these types! Here's the code I used to validate there are no compiler errors:

                              struct link
                              {
                              struct link* next;
                              };
                              struct node
                              {
                              int a;
                              link b;
                              int c;
                              };
                              int main()
                              {
                              node y = { 3, nullptr, 2 };
                              node x = { 5, &y.b, 4 };
                              node* p = list_next_entry(&x, b);
                              cout << "value = " << p->a << endl;

                              return 0;
                              

                              }

                              It did compile, but the output was some random number, not 3 as I expected. I suspect that either the structs or that second #define must be defined differently... P.P.S.: it compiles and prints value = 3 as expected.

                              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                              _ 2 Replies Last reply
                              0
                              • L Lost User

                                What do you think decltype is going to do for you?

                                _ Offline
                                _ Offline
                                _Flaviu
                                wrote on last edited by
                                #28

                                I have nver used decltype by now, I guess I have used improperly ... I have to read about this.

                                1 Reply Last reply
                                0
                                • S Stefan_Lang

                                  Took me a while to figure it out, but decltype(*pos) will give you a reference type, and the parts of the macro using this type to cast to type* then fail, because you can't create a pointer to a reference! However, that's where std::remove_reference comes to the rescue! Try these definitions:

                                  #include // for std::remove_reference

                                  using namespace std;

                                  #define list_entry(ptr, type, member) \
                                  ((type *)((char *)(ptr)-(size_t)(&((type *)nullptr)->member)))
                                  #define list_next_entry(pos, member) \
                                  list_entry((pos)->member.next, remove_reference< decltype(*(pos)) >::type, member)

                                  P.S.: are you sure the second #define you posted is correct? I could make up some type definitions that I could use with these macros without encountering compiler errors, but I'm not at all sure the macros would provide a valid pointer for these types! Here's the code I used to validate there are no compiler errors:

                                  struct link
                                  {
                                  struct link* next;
                                  };
                                  struct node
                                  {
                                  int a;
                                  link b;
                                  int c;
                                  };
                                  int main()
                                  {
                                  node y = { 3, nullptr, 2 };
                                  node x = { 5, &y.b, 4 };
                                  node* p = list_next_entry(&x, b);
                                  cout << "value = " << p->a << endl;

                                  return 0;
                                  

                                  }

                                  It did compile, but the output was some random number, not 3 as I expected. I suspect that either the structs or that second #define must be defined differently... P.P.S.: it compiles and prints value = 3 as expected.

                                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                                  _ Offline
                                  _ Offline
                                  _Flaviu
                                  wrote on last edited by
                                  #29

                                  Soon as I put

                                  #include // for std::remove_reference

                                  in file.h I got:

                                  error C2894: templates cannot be declared to have 'C' linkage

                                  for more than 100 times ... what settings should I use to get rid of this error ?

                                  S 1 Reply Last reply
                                  0
                                  • _ _Flaviu

                                    Soon as I put

                                    #include // for std::remove_reference

                                    in file.h I got:

                                    error C2894: templates cannot be declared to have 'C' linkage

                                    for more than 100 times ... what settings should I use to get rid of this error ?

                                    S Offline
                                    S Offline
                                    Stefan_Lang
                                    wrote on last edited by
                                    #30

                                    See here: visual c++ - error C2894: templates cannot be declared to have 'C' linkage - Stack Overflow[^] Make sure that type_traits gets included outside of the

                                    extern "C" { ... }

                                    block.

                                    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                                    1 Reply Last reply
                                    0
                                    • S Stefan_Lang

                                      Took me a while to figure it out, but decltype(*pos) will give you a reference type, and the parts of the macro using this type to cast to type* then fail, because you can't create a pointer to a reference! However, that's where std::remove_reference comes to the rescue! Try these definitions:

                                      #include // for std::remove_reference

                                      using namespace std;

                                      #define list_entry(ptr, type, member) \
                                      ((type *)((char *)(ptr)-(size_t)(&((type *)nullptr)->member)))
                                      #define list_next_entry(pos, member) \
                                      list_entry((pos)->member.next, remove_reference< decltype(*(pos)) >::type, member)

                                      P.S.: are you sure the second #define you posted is correct? I could make up some type definitions that I could use with these macros without encountering compiler errors, but I'm not at all sure the macros would provide a valid pointer for these types! Here's the code I used to validate there are no compiler errors:

                                      struct link
                                      {
                                      struct link* next;
                                      };
                                      struct node
                                      {
                                      int a;
                                      link b;
                                      int c;
                                      };
                                      int main()
                                      {
                                      node y = { 3, nullptr, 2 };
                                      node x = { 5, &y.b, 4 };
                                      node* p = list_next_entry(&x, b);
                                      cout << "value = " << p->a << endl;

                                      return 0;
                                      

                                      }

                                      It did compile, but the output was some random number, not 3 as I expected. I suspect that either the structs or that second #define must be defined differently... P.P.S.: it compiles and prints value = 3 as expected.

                                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                                      _ Offline
                                      _ Offline
                                      _Flaviu
                                      wrote on last edited by
                                      #31

                                      It works ! :) Kindly thank you all of you ! Without you I would not succedded !

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups