-3

I am currently working on a leetcode question, and try to track down the code process in my end, this is my solution:

#include <iostream>
#include <vector>
#include <stack>
#include <utility>

using namespace std;

vector<int> direction{-1, 0, 1, 0, -1};

int maxAreaOfIsland(vector<vector<int>>&grid){

  int m = grid.size(), n = m ? grid[0].size() : 0, local_area, area = 0, x, y;
  for (int i = 0; i < m; ++i){

    for (int j = 0; j < n; ++j){

      if(grid[i][j]){
        local_area = 1;
        grid[i][j] = 0;
        stack<pair<int, int>> island;
        island.push({i, j});
        while(!island.empty()){
          auto [r, c] = island.top(); \\problem line, vscode can't understand it
          island.pop();
          for (int k = 0; k < 4; ++k){
            x = r + direction[k], y = c + direction[k + 1];
            if(x>=0 && x<m && y>=0 && y<n && grid[x][y]==1){
              grid[x][y] = 0;
              ++local_area;
              island.push({x, y});
            }
          }
        }
        area = max(area, local_area);
      }

    }
  }

  return area;
}

this code works on the leetcode side, but not mine, here is the warning

[Running] cd "c:\Users\chen1\OneDrive\Desktop\C_C++tut\" && g++ leetcode695.cpp -o leetcode695 && "c:\Users\chen1\OneDrive\Desktop\C_C++tut\"leetcode695
leetcode695.cpp: In function 'int maxAreaOfIsland(std::vector<std::vector<int> >&)':
leetcode695.cpp:23:16: warning: structured bindings only available with -std=c++17 or -std=gnu++17
           auto [r, c] = island.top();
                ^
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

[Done] exited with code=1 in 0.932 seconds

Can someone explains why, although I get an alternative way to replace it, it is still annoying and perplexing

thanks for helping

additionally!!! I actually have my main function; the problem here is a syntax error where leetcode's compiler recognizes it, but not g++, the line that causes the problem is auto [r, c] = island.top();, if I alter it to

int r = get<0>(island.top());
int c = get<1>(island.top());

then it works fine, I just don't understand why leetcode compiler can understand it, but not g++

LuCC
  • 40
  • 7
  • 2
    You are trying to compile against an incompatible standard. Try adding `-std=c++17` to the build command. As your compiler tells you: `warning: structured bindings only available with -std=c++17 or -std=gnu++17` – Lala5th Jul 31 '21 at 17:19
  • The problem has nothing to do with vscode. vscode is a text editor not the compiler. The compiler you are using is the MinGW-w64 flavour of gcc. – Clifford Jul 31 '21 at 17:22
  • 2
    @Lala5th That is true, but only a warning; the build failure is `undefined reference to `WinMain'`. – Clifford Jul 31 '21 at 17:25
  • You only have to write solver function in LeetCode, that is not sufficient to build/run the program on your local machine. Write the `main` function to handle input and output of your program and pass the necessary data to your function (`maxAreaOfIsland`). For the standard related warning refer this: https://stackoverflow.com/a/55131548 – brc-dd Jul 31 '21 at 17:28
  • @Lala5th : It is not my code. I am not the OP. I have already pointed that out in my answer. – Clifford Jul 31 '21 at 17:39
  • The question about the warning is a duplicate of https://stackoverflow.com/questions/63300005/g-compiler-warning-when-using-c-17-updates, but that is not why your build is failing. The message is also misleading since the compiler goes ahead and allows it in any case even in C++14 - it just warns you that it is not C++14 syntax. – Clifford Jul 31 '21 at 17:48
  • @Lala5th so can I understand in this way the line ```auto [r, c] = island.top();``` is only allow in c++ 17, so I need to tell the compiler to compile it in c++17 way? – LuCC Jul 31 '21 at 18:53
  • It was introduced in C++17, the default for gcc is probably C++14 (depends on version). It is a warning not an error, so to say it is "_only allow in c++ 17,_" is incorrect - although that is what the message implies - seems to be misleading. It is simply saying that your are using C++17 syntax in C++14, but it is _permissive_ and will compile it in any event. You can continue and ignore it or set `-std=c++17`. I am not sure why this is so confusing to you - the message tells you exactly what to do to resolve it. – Clifford Jul 31 '21 at 19:02

1 Answers1

0

The linker error (not the warning) is what is causing the build to fail (scroll sideways!):

[...]crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'

occurs in MinGW gcc when your code lacks either a main() or WinMain() entry point. As yours does. I guess leetcode (which I have never heard of or used) provides a test harness for you to run the function? It compiles - the message is a linker error, so it cannot form an executable - you need a main().

With respect to the warning, again you need to scroll to the end of the message:

leetcode695.cpp:23:16: warning: structured bindings only available 
                                with -std=c++17 or -std=gnu++17
           auto [r, c] = island.top();

can be resolved by specifying C++17 (or higher) compilation (or not using structured bindings). A Windows/VSCode specific solution is discussed at G++ Compiler warning when using c++ 17 updates, but fundamentally it is about setting the compiler switch -std=c++17.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • I am sorry, I actually have the main(), and it seems like just a syntax error, where the leetcode compiler recognize it, but not g++, for example, if I alter the line "auto [r, c] = island.top();" to " int r = get<0>(island.top()); int c = get<1>(island.top());" it works fine – LuCC Jul 31 '21 at 18:42
  • sorry, you are right, it solves my problem, thanks – LuCC Jul 31 '21 at 18:54
  • ... so why is the linker is reporting "`undefined reference to `WinMain'`"? If it "works fine" with the change then clearly you are doing something that you were not doing in the original log. If you were not interested in the linker error, you should have omitted it from your question. Why would your change your code rather then switching the compilation option? Besides it was a warning - the code was compiling in any event, so it "works fine" in any case. Little about your question or your solution makes much sense now. Closing as a duplicate. – Clifford Jul 31 '21 at 18:54