InsaneCoder
InsaneCoder

Reputation: 8268

Getting error in inheriting class in C++: template argument deduction/substitution failed

I am a newbie to c++.

I have written a very simple program which is as below

#include<iostream>

using namespace std;

class index
{
protected:
    int count;
public:
    index()
    {
        count=0;
    }
    index(int c)
    {
        count=c;
    }
    void display()
    {
        cout<<endl<<"count="<<count;
    }
    void operator ++()
    {
        count++;
    }
};

class index1:public index{
public:
    void operator --()
    {
        count--;
    }
};

int main()
{
    index1 i;
    i++;
    cout<<endl<<"i="<<i.display();
    i++;
    cout<<endl<<"i="<<i.display();
    i--;
    cout<<endl<<"i="<<i.display();
}

But when I compile this code in G++, I get this:

In file included from /usr/include/c++/4.7/iostream:40:0,
                 from inheritance.cpp:1:
/usr/include/c++/4.7/ostream:480:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/include/c++/4.7/ostream:480:5: note:   template argument deduction/substitution failed:
inheritance.cpp:40:30: note:   cannot convert ‘i.index1::<anonymous>.index::display()’ (type ‘void’) to type ‘char’

EDIT I changed cout<<endl<<"i="<<i.display(); to cout<<endl<<"i="; i.display(); and it solved the problem.

But now I am getting

inheritance.cpp:39:3: error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]

Upvotes: 1

Views: 602

Answers (4)

Tomas Kubes
Tomas Kubes

Reputation: 25098

You should put the std::ostream& stream parameter into display function:

std::ostream& display(std::ostream& stream)
{
   stream << endl << "count=" << count;
   return stream;
}

Then you can display the write the object into standard output or into file.

Upvotes: 0

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

You can not pass a void function to an iostream.

Either you function should return a value or an iostream or display() write itself something (like it seems to be). You can solve your problem by doing :

int main()
{
    index1 i;
    i++;
    cout<<endl<<"i=";
    i.display();
    i++;
    cout<<endl<<"i=";
    i.display();
    i--;
    cout<<endl<<"i=";
    i.display();
}

Also your operator++ overloading is wrong, it should be :

index operator ++(int)    // Look at the return value
{
    count++;
    return *this;       // return
}

Same thing for the operator--.

Just take a look at this for operator overloading.

Upvotes: 2

Kunal
Kunal

Reputation: 3535

Following line mean you are appending void to stdout and that is not supported.

cout<<endl<<"i="<<i.display();

So compiler complains as below.

"cannot convert ‘i.index1::<anonymous>.index::display()’ (type ‘void’) to type ‘char’"

You can do the same with following,

cout<<endl<<"i=";
i.display();

Upvotes: 0

aschepler
aschepler

Reputation: 72271

A g++ error message that begins with note: is just providing more information about why a previous error happened. With g++ 4.8, I get (among other errors):

main.cpp:40:21: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘void’)
     cout<<endl<<"i="<<i.display();
                     ^

which pretty well explains the problem. The type of i.display() is void, so you can't pass it to operator<< like that.

Upvotes: 0

Related Questions