Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling in C++ a non member function inside a class with a method with the same

Tags:

c++

c

I have this class with an instance method named open and need to call a function declared in C also called open. Follows a sample:

void SerialPort::open()
{
    if(_open)
        return;
    fd = open (_portName.c_str(), O_RDWR | O_NOCTTY ); 
    _open = true;
}

When I try to compile it (using GCC) I get the following error:

error: no matching function for call to 'SerialPort::open(const char*, int)'

I included all the required C headers. When I change the name of the method for example open2 I don't have not problems compiling.

How can I solve this problem. Thanks in advance.

like image 413
jassuncao Avatar asked Feb 05 '10 13:02

jassuncao


People also ask

Can we call a non-member function inside a class?

The answer is YES.

How do you call a function method that is a member of a class?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.

Can any member function of the class call to other member function of the same class?

Explanation: We can call one function inside another function to access some data of class. A public member function can be used to call a private member function which directly manipulates the private data of class.

Which section of a class can a non-member function access?

Non-member functions are instead declared outside any class (C++ calls this "at namespace scope").


1 Answers

Call

fd = ::open(_portName.c_str(), O_RDWR | O_NOCTTY );

The double colon (::) before the function name is C++'s scope resolution operator:

If the resolution operator is placed in front of the variable name then the global variable is affected.

like image 91
unwind Avatar answered Sep 17 '22 18:09

unwind