1

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]

TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
Naveen
  • 7,944
  • 12
  • 78
  • 165
  • 2
    You're trying to pass the return value of a *void* function to an iostream... You probably meant `cout << endl << "i="; i.display();` – Borgleader Aug 27 '13 at 10:10
  • @Borgleader:Thanks ,it solved the problem .But it cannot find my overloaded operators.`inheritance.cpp:42:3: error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive] ` – Naveen Aug 27 '13 at 10:15
  • post fix operator can be overloaded by operator++ (int) syntax. – Kunal Aug 27 '13 at 10:20
  • Your second question is discussed at http://stackoverflow.com/questions/894804 – aschepler Aug 27 '13 at 10:20

4 Answers4

2

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.

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
0

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
0

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();
Kunal
  • 3,475
  • 21
  • 14
0

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.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148