Inheritance

Can someone PLEASE HELP ME!! I am trying to get access to the sorted array in the Mode class, so that I can find the median of the array. The program runs before I created the Statistics class. But I get an error when I try to inherit the Mode class.
Last edited on
closed account (zb0S216C)
It's telling you that the compiler cannot find a Mode constructor with int* and int as parameters. Declare and define a Mode constructor that takes no arguments.

Wazzak
Last edited on
Sorry I am a newbie. Where would I declare and define a Mode constructor?
closed account (zb0S216C)
The same place where Mode(int *,int) is declared but underneath or above it. For example:

1
2
3
4
5
6
7
8
9
class Mode
{
    public:
        // Default constructor:
        Mode( );
       
        // Other constructors:
        Mode( int *, int );
};


Wazzak
I get another error after I added the Default constructor Mode();

Mode.h:62: undefined reference to `Mode::Mode()'
closed account (zb0S216C)
Could you post your code along with [_code_][_/code_] tags please (remove the underscores). Before you do, could you check if you've place a set of parentheses ((...)) after the symbol of the instantiation. If you have, remove them.

Wazzak
ok
Last edited on
closed account (zb0S216C)
The error doesn't reside within the code you posted. Can you post the contents of main( )? This time, can you use this link: http://pastebin.com/ It saves a whole lot of space.

Wazzak
Now I get an error:
Mode.cpp:111:5: error: prototype for ‘int Statistics::set_Median()’ does not match any in class ‘Statistics’
Last edited on
closed account (zb0S216C)
What line if the compiler referring to?

Wazzak
Error:
Mode.cpp:85:5: error: prototype for ‘int Statistics::set_Median()’ does not match any in class ‘Statistics’
Mode.h:73:9: error: candidate is: int Statistics::set_Median() const
Last edited on
closed account (zb0S216C)
You need to place the const qualifier after the function interface (after the parameter list and before the opening brace) of Statistics::set_Median( ). For example:

1
2
3
4
5
// Mode.cpp

int Statistics::set_Median( ) const
{
}


Wazzak
I get another error:
Mode.cpp:85:5: error: redefinition of ‘int Statistics::set_Median() const’
Mode.h:73:9: error: ‘int Statistics::set_Median() const’ previously defined here
closed account (zb0S216C)
That's because you've already defined a body for Statistics::set_Median( ) within Mode.h. Make sure you omit the braces. For example:

1
2
3
4
5
class Mode
{
    // Should look like this:
    int set_Median( ) const;
};


Wazzak
Mode.h:61:23: error: no matching function for call to ‘Mode::Mode()’
Mode.h:23:6: note: candidates are: Mode::Mode(int*, int)
Mode.h:10:12: note: Mode::Mode(const Mode&)
The constructor of the child class calls at the constructor of the parent.
You didn't define any constructor for Statistics, so the compiler created a default one, and it's trying to use the default constructor of Mode (that it does not exists)
Solution: create a proper constructor for Statistics Statistics::Statistics(): Mode( proper, parameters ){ //...

1
2
//Mode *mode=new Mode(one_Dim,array_size); //don't do this crap
Mode mode(one_Dim, array_size);


¿why does 'Mode' have a set_Median() method?
Last edited on
After I removed the braces I get Error:
Mode.cpp:86:20: error: assignment of data-member ‘Statistics::middle’ in read-only structure
closed account (zb0S216C)
protected members of a class are only accessible by the inherited class and by the class that declared them. Therefore, any variable of function (that is not associated with the class) attempting to access protected members are rejected. You need to make Statistics::middle public, or, create a method that returns a non-constant reference to Statistics::middle. However, this will break encapsulation.

Wazzak
Last edited on
I tried to call the get_Median from the Main.cpp and I get an error.

Mode.h:62: undefined reference to `Mode::Mode()'
Last edited on
Please update your code. Also post the class declaration.
some_method( ) const; is telling that it will not modiify the object. You are violating that in set_Median, ¿does it make sense for set_Median to be const?

You never defined a default constructor for Mode. ¿does it make sense to provide one?
I have been working on this program all day and I can't get this program to run. After I fix an error I just keep getting new errors. I am trying to call get_Median but I get an error Please can anyone help?
Last edited on
Topic archived. No new replies allowed.