Won't call a const member function

D

Dave C

I've written the following code that won't compile, trimmed down to
just the pertinent stuff:

--- WindowClass.hxx ----------------------------------------------------
#include <set>

class Window;

class WindowClass {
public:
addWindow (Window const* const window);
private:
static std::set<Window*> s_constructing;
};
------------------------------------------------------------------------

--- WindowClass.cxx ----------------------------------------------------
#include "WindowClass.hxx"

using namespace std;

void WindowClass::addWindow (Window const* const window) {

if (s_constructing.count(window) > 0) {
<snip>
}

}
------------------------------------------------------------------------

The error I get is on the line:

if (s_constructing.count(window) > 0) {

The compiler tells me that there's no match for my call to .count, but
it has a near match const function. The thing I don't understand is,
why wouldn't my non-const function call a const function. I know it
won't work the other way around, but that's not what I'm trying to do.
I can get it to compile if I remove const from addWindow()'s
parameter, but I want to make this const correct. Can anyone enlighten
me?
 
X

Xenos

Dave C said:
I've written the following code that won't compile, trimmed down to
just the pertinent stuff:

--- WindowClass.hxx ----------------------------------------------------
#include <set>

class Window;

class WindowClass {
public:
addWindow (Window const* const window);
private:
static std::set<Window*> s_constructing;
};
------------------------------------------------------------------------

--- WindowClass.cxx ----------------------------------------------------
#include "WindowClass.hxx"

using namespace std;

void WindowClass::addWindow (Window const* const window) {

if (s_constructing.count(window) > 0) {
<snip>
}

}
You have a pointer to a constant object that you are trying to pass to a
member of a set of pointers to a non-constant object. You are trying to
implicitly convert a constant window to a window, and the compiler is tell
you that that would be bad.
 
P

Phillip Mills

The compiler tells me that there's no match for my call to .count, but
it has a near match const function. The thing I don't understand is,
why wouldn't my non-const function call a const function.

I believe it is not complaining about the function, but about the data
type. Your s_constructing set would need to be...

static std::set<Window const*> s_constructing;

....for the 'count' to be happy.
 
O

Old Wolf

class WindowClass {
public:
addWindow (Window const* const window); ^^^^^ this is ignored
private:
static std::set<Window*> s_constructing;
};

void WindowClass::addWindow (Window const* const window) {

The second 'const' here is mostly useless, if you can avoid
problems by removing it, then do so.
if (s_constructing.count(window) > 0) {

set<T>::count() expects a 'T *'. You can't convert
(const Window *) to (Window *) implicitly (it only works the
other way around, as you noted). You should use const_cast
to fix this:

if (s_constructing.count(const_cast<Window *>(window)) > 0) {
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top