diox8tony
diox8tony

Reputation: 346

compiler error, when MyClass::read() and unistd.h read(int, void*,size_t) are both used (same name of global function)

In this example:

#include <unistd.h>
MyClass {
  void read();
};

void MyClass::read() {
  read(int,void*,size_t); // <- valid call to unistd.h version of read
}

My compiler says:

Error: MyClass::read(int, void*, size_t) is not a function; suggest MyClass::read() instead

If I change the name of MyClass::read_2(), the compiler finds unistd.h::read(int,void*,size_t) just fine.

I thought the compiler checks all valid scopes (closest scope first, then ascending order upwards to global/libraries) to find the matching function? Maybe spit out a warning saying it found alternatives.


EDIT: oh wow:

::read(int,void*,size_t) 

WORKS without changing the name of MyClass::read(). I guess I forgot about explicit global namespacing/scoping. But still, why wouldn't the compiler just do the search for me? Is it forcing explicit to reduce mistakes? Why would it get hung up on a non-matching function when a matching one exists at a higher scope?

Debian 9 (stretch), gcc 6.3.0, c++

Upvotes: 0

Views: 34

Answers (0)

Related Questions