-5

I'm trying to make a program that keeps track of how many times a sort does a comparison but I'm not sure how to pass a variable to and from the base class to the child class. Right now I get an error saying "error: passing 'const childsort' as 'this' argument of 'void AbstractSort::setConversions(int)' discards qualifiers [-fpermissive]|"

I have a few questions. Isn't the purpose of using a child class that it inherits the functions from the parent class? If so how do I get access to the conversion variable in the base class through the child class? If not, do I need a local variable and a local function in the child class to count the conversions?

#include <iostream>
using namespace std;

class AbstractSort
{
protected :
    int conversions;

public :
    AbstractSort(){};

    void setConversions(int c)
    { conversions = c; }

    int getConversions() const
    { return conversions; }

    int addConversions()
    { return conversions++; }

    virtual void sort(int arr[], int size) const = 0;
};


class childsort : public AbstractSort
{
    public :
        childsort() : AbstractSort(){}

        virtual void sort(int arr[], int size) const
        {
            int x = 0, y = 0, c = 0;

            for(x = 0; x < size - 1; ++x)
            {
                for(y = 0; y < size - x - 1; ++y)
                {
                    if(arr[y] > arr[y + 1])
                    {
                        int temp = arr[y];
                        arr[y] = arr[y + 1];
                        arr[y + 1] = temp;
                    }
                setConversions(c++);
                }
            }
        }
};
cba1067950
  • 35
  • 1
  • 11

2 Answers2

1

Q: Isn't the purpose of using a child class that it inherits the functions from the parent class?

A: Yes, it is. The methods in the base class must be "public" or "protected". Additionally, the child class may override or hide the corresponding base class method(s).

In your case, the problem was a simple typo: setConversions() vs setConversion().

===========================================

Addendum: you fixed the typo, then you got this (new!) error:

"const childsort as 'this' argument of 'void AbstractSort::setConversions(int)' discards qualifier..."

This is because of the conflict between sort() .. const and setConversions().

Look here (for a similar case):

error: passing xxx as 'this' argument of xxx discards qualifiers

. So when you try to call [your method] with the const object the compiler detects a problem, namely you're calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object; so the compiler is going to make a safe assumption that [your method] might attempt to modify the object but at the same time, it also notices that the object is const; so any attempt to modify the const object should be an error. Hence compiler generates error message.

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I fixed the typo and I'm getting a different error now. I edited the question with the current error. – cba1067950 Oct 24 '16 at 00:26
  • New new error. Removing the const now gives me an error in my main function. I created an object "childsort q;" and it says "invalid abstract return type 'childsort' childsort q(); gives the same error. There's a lot of movie pieces for a newbie. Sorry if that this is stupid. <3 – cba1067950 Oct 24 '16 at 00:39
  • How are you supposed to give answers to the FAQ's if the purpose of the site isn't to give answers? Imagine a teacher put question limits on their students and only answered everything in the context of one base example. You'd never learn anything. – cba1067950 Oct 24 '16 at 00:57
  • I think I figured it out so for the sake of this wonderful database of half answers, removing the "const" from the parent class' virtual function solves the last issue. I'm guessing, because I'm no expert, but if the parent class has a function set to "const" a child class can't be created if the inherited function in the child changes some value in the parent function. – cba1067950 Oct 24 '16 at 01:57
  • Dude:if you want to understand "const", start with the SO thread I cited, Google "C++ const", or - heaven forbid - pick up a book and read it! The simple, concise answer is exactly what πάντα ῥεῖ said: "You can't use a non const member function in a const member function". – paulsm4 Oct 24 '16 at 05:44
-1

Your compilation error is due to the fact that

virtual void sort(int arr[], int size) const

Is a const method, which is calling the non-const setConversions() method.

A const method can only call other const methods, whether in its own class or in the superclass. Additionally, a const method cannot modify any non-mutable class members.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148