Re: Odd error I can't seem to recreate
On 11/4/2011 2:52 PM, Noah Roberts wrote:
In a significant code base I'm running into this problem:
This works:
test_service service;
std::string result = job->run_query(service);
This does not.:
std::string result = job->run_query(test_service());
the run_query function expects a const reference to test_service's
base class:
struct location_job
{
virtual ~location_job() {}
virtual std::string run_query(location_service const& service) const
= 0;
};
Seems like there is some kind of relationship between 'location_service'
and 'test_service', but you decided to omit it. Perhaps you could
elaborate? Is 'test_service' derived from 'location_service' as private
or protected?
The error I get from g++4.1.2 is:
pipe_processor_test.cpp:142: error: no matching function for call to
?test_service::test_service(test_service)?
../inc/test_service.h:27: note: candidates are:
test_service::test_service(bool)
../inc/test_service.h:7: note:
test_service::test_service(test_service&)
Is there the default c-tor for 'test_service'? I presume there is.
So, 'test_service' copy constructor takes a non-const reference. To
initialize the argument to your 'run_query', a temporary of type
'location_service' has to be created (or has to be able to be created),
and I am guessing there is no conversion that the compiler can use.
When you supply a "real" object, not a temporary, the derived-to base
conversion is used, and all is well.
The bool constructor is made by me, but the non-const copy constructor
is the compiler's.
What??? Why would the compiler create a non-const copy c-tor? Or,
rather, what have you done to tell it to create that particular form of
the copy c-tor?
> Line 7 is the opening brace for the class.
I can't make any sense of this. Does anyone have ANY idea what might
be going on so I can try to narrow it down and create a minimal
example to get better help? The attempts I've made so far work
fine.
Under what conditions in which the first could work might the second
not when the parameter is const ref? I can't think of any.
Thanks for any help that can be provided. I know it's a reach.
Please try to narrow it down by copying your classes into a separate
module and gradually removing all that is irrelevant, while still trying
to repeat the compiler behavior. Then post the remaining source, which
by that time should be no larger than this:
struct loc {
loc() {}
loc(loc&) {}
};
struct test : loc {
test(test& t) : loc(t) {}
test() : loc() {}
};
void foo(const loc&);
int main()
{
foo(test()); // and here you should get an error
test t;
foo(t);
}
I don't get the error with Comeau online for this program, but (a) I was
just guessing, and (b) that is a different compiler.
V
--
I do not respond to top-posted replies, please don't ask