We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

cannot convert 'this' pointer from......  error

benoost123
benoost123 asked
on
Medium Priority
2,270 Views
Last Modified: 2008-01-09
This function has to go thru a container of shape *, check if the shape is inside the rectangle and if it is
draw it.

template <typename Container>
void draw_all_seq_inside(const Container &con, const Rectangle &w) {
      
      complex<double> bl(static_cast<double>(w.getBlX()), static_cast<double>(w.getBlY()));
      complex<double> tr(static_cast<double>(w.getTrX()), static_cast<double>(w.getTrY()));
      Container::const_iterator iter;
      for(iter=con.begin(); iter!=con.end(); ++iter) {
            if((*iter)->inside_window(bl, tr)) {            
                  (*iter)->draw();
            }
      }
}

I get this error when compiling...

c:\C++\coursework\shape_utils.h(32): error C2662: 'Rectangle::getBlX' : cannot convert 'this' pointer from 'const Rectangle' to 'Rectangle &'
...gives the same errors for the getBlY, getTrX and getTrY...

Why does this happen..?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
the iterator returns a const class Rectangle, but the member-function getBLX is not declared with const access.
rewrite that methos to:

class Rectangle
{
public:
    int getBlX() const;
};

int Rectangle::getBlX() const
{
    return blx;
}

member-function, which doesnt manupulate any class-member-variables should always be declared as const.
look at this:

void anyfunction( const Rectangle &r )
{
    // now we can access this method even with a const-Rectangle-Object..
    int blx = r.getBLX();
}

hope it helps :)
ike

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2009

Commented:
You need to define the member functions of the Rectangle class as const :

  ret_type getBlY() const { /* ... */ }

ie. add the const before the function body.
CERTIFIED EXPERT

Commented:
btw:  what about this:

class Rectangle
{
public:
    complex<double> getBL() const { return complex<double>( static_cast<double>(w.getBlX()), static_cast<double>(w.getBlY()) ); }
    complex<double> getTR() const { return complex<double>( static_cast<double>(w.getTrX()), static_cast<double>(w.getTrY()) ); }
};


so you hide it in the classs and have more nice accessors in your code:

emplate <typename Container>
void draw_all_seq_inside(const Container &con, const Rectangle &w) {
     
     complex<double> bl = w.getBL()
     complex<double> tr = w.getTR()

     Container::const_iterator iter;
     for(iter=con.begin(); iter!=con.end(); ++iter) {
          if((*iter)->inside_window(bl, tr)) {            
               (*iter)->draw();
          }
     }
}

and why not create a struct/class point, so you dont need to *obuse* the complex-type. this can be confusing,
because its not a complex-number, its just a point ... :)

ike

Author

Commented:
Thanks again ikework.... that helps alot!

Author

Commented:
And now for the for_each one...  :-)
CERTIFIED EXPERT

Commented:
good luck, you'll get it :))
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.