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..?
benoost123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.