1

I'm making my own little project. Recently, as to add some features, I decided to add these 2 functions. Here is the code

//filename: tictactoe.cpp
bool is_empty(char cell[][3]){
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            if (cell[j][i] != '\0') return false;
        }
    }
    return true;
}
bool strtest(char cell[3][3]){
    if (is_empty(cell)) return true;
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            if (cell[j][i] == '\0') return false;
        }
    }
    return true;
}

Here is the error in it's full glory (compiled with g++ version 10.3.0 on Ubuntu 21.04):

tictactoe.cpp: In function ‘bool strtest(char (*)[3])’:
tictactoe.cpp:68:6: error: reference to ‘is_empty’ is ambiguous
   68 |  if (is_empty(cell)) return true;
      |      ^~~~~~~~
In file included from /usr/include/c++/10/bits/move.h:57,
                 from /usr/include/c++/10/bits/nested_exception.h:40,
                 from /usr/include/c++/10/exception:148,
                 from /usr/include/c++/10/ios:39,
                 from /usr/include/c++/10/ostream:38,
                 from /usr/include/c++/10/iostream:39,
                 from tictactoe.cpp:1:
/usr/include/c++/10/type_traits:715:12: note: candidates are: ‘template<class _Tp> struct std::is_empty’
  715 |     struct is_empty
      |            ^~~~~~~~
tictactoe.cpp:59:6: note:                 ‘bool is_empty(char (*)[3])’
   59 | bool is_empty(char cell[][3]){
      |      ^~~~~~~~
Compilation failed.

Do you have any idea how to fix this? I tried and can't find anything about this error on SO. The headers I use are only <ncurses.h> and some standard headers.

  • 1
    Could it be that you are using `using namespace std` somewhere in your code? – t.niese Oct 02 '21 at 07:37
  • `::is_empty(cell)` – bolov Oct 02 '21 at 07:37
  • @t.niese Yes, I do – Minh Đức Hoàng Oct 02 '21 at 08:11
  • What you encounter here is the reason why `using namespace` can be problematic and especially why `using namespace std` is considered bad practice. ([Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721)). In the `std` you have a struct `is_empty` and you have your function `is_empty` and due to the `using namespace` the compiler does not know which one to use. – t.niese Oct 02 '21 at 08:15
  • Okay, the problem is me being stupid. I have read somewhere that `using namespace std` is bad practice and you should use `using std::whatever_you_want`. I'm not really used to the way c++ throws everything into the std namespace and doesn't automatically include it from the start like Python – Minh Đức Hoàng Oct 02 '21 at 08:21
  • That would also happen in other programming languages in which you have a command that would allow you to commit some kind of prefix. – t.niese Oct 02 '21 at 09:17

0 Answers0