LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2010, 04:48 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
g++ error message no matching function for call to some function


Hi all,

I got a confusing error message when compiling the C++ code:
Code:
]$ g++ -c testcls.cc -o testcls.o
testcls.cc: In member function `int testcls::open(const char*)':
testcls.cc:14: error: no matching function for call to `testcls::open(const char*&, int)'
testcls.cc:13: note: candidates are: int testcls::open(const char*)
and the (simplified) code is:
Code:
$ cat testcls.cc
class testcls
{
public:
	int open( const char *fn );
};

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

int testcls::open( const char *fn )
{
	return open( fn, O_RDONLY );
}
If the member function is renamed to something else, the compiler won't complaint anymore. Why? and how to work around without modifying the function name? Please.

Code:
$ g++ --version
g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-4)
Copyright (C) 2006 Free Software Foundation, Inc.

Last edited by Jerry Mcguire; 04-13-2010 at 04:53 AM.
 
Old 04-13-2010, 05:50 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Jerry Mcguire View Post
Hi all,

I got a confusing error message when compiling the C++ code:
Code:
]$ g++ -c testcls.cc -o testcls.o
testcls.cc: In member function `int testcls::open(const char*)':
testcls.cc:14: error: no matching function for call to `testcls::open(const char*&, int)'
testcls.cc:13: note: candidates are: int testcls::open(const char*)
and the (simplified) code is:
Code:
$ cat testcls.cc
class testcls
{
public:
	int open( const char *fn );
};

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

int testcls::open( const char *fn )
{
	return open( fn, O_RDONLY );
}
If the member function is renamed to something else, the compiler won't complaint anymore. Why? and how to work around without modifying the function name? Please.

Code:
$ g++ --version
g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-4)
Copyright (C) 2006 Free Software Foundation, Inc.
http://www.cplusplus.com/reference/i.../fstream/open/
 
Old 04-13-2010, 06:15 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Sergei

Any special reasons you pointed out open() function of fstream ?

Why you didn't point this normal open() function

And I still could'nt understand why compiler couldn't distinguish between the open function of that class and the normal UNIX open function ?
 
Old 04-13-2010, 09:49 AM   #4
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
nice link, but not very related in my case I guess.

The function prototypes are different in my example. Can't figure out why the compiler doesn't distinguish the two.

Code:
int testcls::open ( const char * );     // class testcls
int open ( __const char *, int , ... ); // fcntl.h

Last edited by Jerry Mcguire; 04-13-2010 at 09:52 AM.
 
Old 04-13-2010, 11:05 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Jerry Mcguire View Post
nice link, but not very related in my case I guess.

The function prototypes are different in my example. Can't figure out why the compiler doesn't distinguish the two.

Code:
int testcls::open ( const char * );     // class testcls
int open ( __const char *, int , ... ); // fcntl.h
In C++ (unlike in "C") function names imply namespace prefix. No prefix, AFAIK, means current class prefix, that's why you get an error. You don't have in your class an 'open' function with the require signature.

If you used 'open' function from the link I provided you with with its namespace prefix, you wouldn't have had the error.

Last edited by Sergei Steshenko; 04-13-2010 at 08:42 PM.
 
Old 04-13-2010, 11:59 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
IIUC, the fact that you used the .h version of the include for C library functions in C++ means it is in global namespace, so it is ::open

The source of the original confusion is that declaring a name in a class cannot overload the same name in global namespace. It masks that name in global namespace.

So you need to use ::open within the class to refer to the global open(). The difference in calling parameters does not prevent the member declaration from masking the global one.
 
Old 04-13-2010, 08:37 PM   #7
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Thanks all. Another mystery solved.

I am doing code porting and trying to keep close to the original. There are obvious reasons not to change using other function or the result may not be desired. Yet, will do consider using fstream in future C++ developments.

Thank you again.

btw, the compiler does know where to find the global open() if my class member function is named differently, which is the trap. Perhaps the compiler may introduce a switch to make the difference.

Last edited by Jerry Mcguire; 04-13-2010 at 08:47 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
g++ complains about function call not matching prototype ETCKerry Programming 8 01-06-2010 08:45 PM
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 10:19 PM
No Matching Function for Call ! vipinsharma Programming 2 07-05-2004 01:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration