Passing const var as argument

I have a class called ShadeRec and a class called World. The ShadeRec constructor takes a reference to a world like this:
ShadeRec::ShadeRec(World& wr)
I need to initialise a new ShadeRec object in a function of the world class, like this:
ShadeRec sr(*this);
However, this gives me a compiler saying that the world is being passed as const. this is the error:
World.cpp:12:19: error: no matching function for call to 'ShadeRec
st World&)'

How can I pass the world-reference as an argument without it being const? All of this code is taken directly from a book I'm following, so I find it a bit weird that it won't compile :p
The function in the World class that is instantiating the ShadeRec must be declared const, hence *this
is of type (const World).

Does ShadeRec's constructor modify World? Should that be a const reference instead?
If this is Ray Tracing from the Ground Up, there are a number of errata listed for const errors: http://www.raytracegroundup.com/errata.html

Virtually all programming books have a website where errata are posted.
Ok, thanks, I wasn't aware of that.

ShadeRec doesn't modify World at all, so it makes sense to make that const instead, like you said. However, when I did that, I got another error:
ShadeRec.cpp:9:13: error: invalid initialization of reference of type 'World&' from expression of type 'const World'


This is the code:
1
2
3
4
5
6
ShadeRec::ShadeRec(const World& wr) : hit_an_object(false),
								local_hit_point(),
								normal(),
								color(0.0),
								w(wr) //this is causing the error {
}

PanGalactic: it is! Thanks, I hadn't looked at that yet:)
Hmm, I couldn't find my problem in the errata either. Why can't I initialize the World-reference the way I do?
Topic archived. No new replies allowed.