• edge [he/him]
      hexagon
      ·
      edit-2
      2 years ago

      My guess is that ChatGPT is just getting it wrong. Here are some of the other errors it gave when I had it retry:

      ref.cpp: In function ‘int main()’:
      ref.cpp:6:12: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘intint& i = k;
                  ^
      ref.cpp:10:12: error: invalid conversion fromint’ to ‘int&’ [-fpermissive]
        if (i == 17) {
                  ^
      

      and

      ref.c: In function ‘int main()’:
      ref.c:7:11: error: ‘int& i’ declared as reference but not initialized
        int& i = k;
                 ^
      

      and

      ref.c: In function ‘int main()’:
      ref.c:7:10: error: ISO C++ prohibits declaration of ‘k’ with no type [-fpermissive]
        int k = 17;
                ^
      ref.c:7:10: error: ISO C++ prohibits declaration of ‘i’ with no type [-fpermissive]
      ref.c:7:14: error: ISO C++ prohibits declaration of ‘i’ with no type [-fpermissive]
      

      I know that last one has to be wrong, int k = 17; is perfectly fine and has a type unlike what the error said.

      Weirdly I can't find where I tried to compile it with gcc and it said I needed to use g++.

      • structuralize_this [none/use name]
        ·
        2 years ago

        The original code is c++, so g++ was the right call. The first couple errors are c++ like, as & isn't part of the type system in C. But it's valid c++. This is the correct error in C, for some new-ish version of gcc:

        main.c: In functionmain’:
        main.c:7:8: error: expected identifier or ‘(’ before ‘&’ token
             int& i = k;^
        main.c:9:5: error: ‘i’ undeclared (first use in this function)
         if (i == 17)
        

        This one is really interesting as it's the most basic bitch variable declaration, but it might get confused because int k probably isn't in the training lexicon.

        ref.c: In function ‘int main()’:
        ref.c:7:10: error: ISO C++ prohibits declaration of ‘k’ with no type [-fpermissive]
          int k = 17;