• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2186
  • Last Modified:

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

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..?
0
benoost123
Asked:
benoost123
  • 3
  • 2
1 Solution
 
ikeworkCommented:
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
0
 
Infinity08Commented:
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.
0
 
ikeworkCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
benoost123Author Commented:
Thanks again ikework.... that helps alot!
0
 
benoost123Author Commented:
And now for the for_each one...  :-)
0
 
ikeworkCommented:
good luck, you'll get it :))
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now