Inaccessible method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    Inaccessible method

    Hi AlI,

    I have a class called Rectangle that contains a public method:
    Code:
    bool contains(int x, int y);
    I have a nother class called Components that is a subclass of Rectangle.

    From a totally different class, Compund, I now want to loop through a vector<Componen t*> and call the method contains with the appropriate arguments.

    I get an error telling me:

    Code:
    ../Rectangle.h:11: `bool Rectangle::contains(int, int)' is inaccessible
    ../Compound.cpp:43: within this context
    It's probably real obvious, I'm not that good with c++.

    Any ideas?

    Cheers
  • Airslash
    New Member
    • Nov 2007
    • 221

    #2
    Originally posted by MarkoKlacar
    Hi AlI,

    I have a class called Rectangle that contains a public method:
    Code:
    bool contains(int x, int y);
    I have a nother class called Components that is a subclass of Rectangle.

    From a totally different class, Compund, I now want to loop through a vector<Componen t*> and call the method contains with the appropriate arguments.

    I get an error telling me:

    Code:
    ../Rectangle.h:11: `bool Rectangle::contains(int, int)' is inaccessible
    ../Compound.cpp:43: within this context
    It's probably real obvious, I'm not that good with c++.

    Any ideas?

    Cheers
    Ok, from my head and on the fly:

    [code=c]
    class Rectangle {
    public:
    virtual bool contains(int, int);
    }
    [/code]

    [code=c]
    class Component : public Rectangle {
    public:
    bool contains(int, int);
    [/code]

    With this your component inherits from rectangle, and overwrites the function.

    An another approach is to insert the rectangle class as a datamember in your Component class, and give your Component class a method contains(int, int) that calls the contains method of your datamember

    Comment

    • MarkoKlacar
      Recognized Expert Contributor
      • Aug 2007
      • 296

      #3
      Hi,

      Thanks for the reply. I created a method inside Component that calls the Rectangle::cont ains(int,int).. .

      Thanks!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by MarkoKlacar
        Thanks for the reply. I created a method inside Component that calls the Rectangle::cont ains(int,int).. .
        That shouldn't be required unless Component::cont ains actually has different functionality to Rectangle::cont ains you should be able to declare it as

        [code=cpp]
        class Rectangle
        {
        public:
        virtual bool contains(int, int);
        }

        class Component : public Rectangle
        {
        }
        [/code]

        and be able to call contains from a object of type Component. I suspect in your first implementation that you left out the public keyword when declaring Component as a subclass of Rectangle.

        Comment

        Working...