illegal use of non static funcion

i'm getting an error which states i have an illegal use of non static funcion?
what's the problem, with my code?

H file

#ifndef Number_h
#define Number_h

#include<iostream>
#include<sstream>
#include<string>
using namespace std;

class Number{
public:
Number(){ cout<<"Number cTor\n"; }
virtual ~Number(){ cout<<"Number dTor\n"; }
virtual void printNumber();
friend ostream& operator<<(ostream&os,Number& num){ Number::printNumber(); return os;} //// here i get the error.
};

#endif

derative h file:

#ifndef Int_h
#define Int_h

#include"Number.h"

class Int : public Number{
protected:
int m_Number;
public:
Int(int const num = 0 ) { cout<<"Int cTor\n"; m_Number = num; } ///Constructor
~Int() { cout<<"Int dTor\n"; }
void printNumber() { cout<<m_Number; } // the is overidinof the print function from Number Class
friend istream& operator >>(istream &is, Int &number) { is>>number.m_Number; return is; }
friend ostream& operator <<(ostream &os,Int const &number) { os<<number.m_Number; return os; }
};

#endif

Instead of Number::printNumber(); you probably meant num.printNumber(); but note that that will always write to cout even if os is something else.
thanks that solved the problem, you mean to write "{ cout<<number.m_Number; return os; }", now have a different error, do you have any idea what it might be:

Error 1 error LNK2001: unresolved external symbol "public: virtual void __thiscall Number::printNumber(void)" (?printNumber@Number@@UAEXXZ) C:\Users\Edward\Documents\Visual Studio 2012\Projects\ex3.1\ex3.1\IntLinkedList.obj ex3.1 ?

thanks a lot for the first one!
You use both printNumber() and operator<< to print the number. I think the reason you have printNumber() is because it can be virtual but operator<< can't be virtual. To be able to use printNumber in operator<< correctly you could let printNumber() take an ostream as argument and pass os from operator<<. If you do that you don't need to overload operator<< for all classes derived from Number.



Error 1 error LNK2001: unresolved external symbol "public: virtual void __thiscall Number::printNumber(void)" (?printNumber@Number@@UAEXXZ) C:\Users\Edward\Documents\Visual Studio 2012\Projects\ex3.1\ex3.1\IntLinkedList.obj ex3.1 ?
This is because you have not implemented the Number::printNumber(). If you don't want to do that, and not be able to have instances of Number, you can declare the function as pure virtual.
virtual void printNumber() = 0;
but i have an implementation for printNumber, in Class Int, void printNumber() { cout<<m_Number; } or it doesn't recognize it as an implementation?
It recognise Int::printNumber() all right. It is Number::printNumber() it complains about. If you don't want to provide an implementation for Number::printNumber() you have to declare it pure virtual.
Topic archived. No new replies allowed.