I am currently porting an app from Sun Solaris where we use the Solaris cc compiler (version 5.8). Have a 12 year old legacy app that compiles "fine" in the current environment :-) While trying to migrate to RedHat Linux and the GNU g++ compiler (version 4.1.2) I am repeatedly running into "error: no matching function for call to ..." errors with code that sends and/or receives reference parameters. Here's an example of the compiler error: EJPeriod.C:102: error: no matching function for call to âEJPeriod::EJPeriod(Date&, Date&, String)â EJPeriod.h:83: note: candidates are: EJPeriod::EJPeriod(EJPeriod&) EJPeriod.h:76: note: EJPeriod::EJPeriod() EJPeriod.h:72: note: EJPeriod::EJPeriod(Date, Date, String&) I don't understand why the compiler doesn't recognize the 3rd constructor referenced by line 72. When I try to overload the constructor to satisfy the compiler complaint the compiler correctly tells me the two constructors create an ambiguous situation. Can you advise as to whether this is a compiler bug or a potentially old coding standard that isn't acceptable with the version of the GNU compiler we are using? Also, this only seems to occur with our "home grown" classes. The compiler has no issue with basic scalar types and the passing and receiving of reference and non-reference parameters. Lastly and admittedly, when I create a simple test.C program to duplicate the issue the simple example compiles. There must be something else in the 1000s of lines of code that I am overlooking. Thanks in advance for any help or direction you can provide. Regards, Marc
Can you show the line where the call to EJPeriod::EJPeriod is done? It might be because String that you are passing is a rvalue which cannot be bound to String& as that would mean it needs to be bound to a lvalue. rvalues can be bound to const references (or in C++0x rvalue references too).
This is the method where I am receiving the compiler error: enum err_stat EJPeriod::rollup( List<EmplJob> &src, List<EJPeriod> &dst, Date &startDt, Date &endDt ) { // then theres a bunch of code // and then the offending line is the one below starting w/EJPeriod if( srcSize == 1 ) { EJPeriod ejptmp( startDt, endDt, src(0)->get_job_title_mgmt_ind() ); dst.add( new EJPeriod( ejptmp ) ); return( Success ); } The object named "src" is a home grown template linked list class.
What type does get_job_title_mgmt_ind return?
Here's the definition for that call ... I think you're on to something String get_job_title_mgmt_ind() const { return _jobTitleMgmtInd; }; When I change the constructor to "non reference" paramters it seems to compile fine. I've got dozens of these to go through. I suspect they're probably all similar. Is there a better fix than removing the reference parameter? I'm not really sure why the old code has it as a reference anyway. The constructor is not looking to change that value anyway ... its just using it to initialize a data member.
Yes GCC is correct as in C++ rvalues don't bind to references as only lvalues bind to references.
If the ctor isn't going to change it, you could as well change the argument type to const String & instead.
Comment 6 is perfect! That makes total sense. Thanks so much! Actually, I bet the Solaris compiler is implicitly treating this same code as a const & String behind the scenes. We'll just explicitly make that promise/fix for this migration. Thanks so much for the speedy and excellent responses!!!