A
Alexandre B
Lately I had some fun trying the figure out how inheritance works in C++,
and I discovered a problem I don't understand. With the following code
(which has been shortened a bit for the ng), gcc output a strange error :
------------------CODE------------------
#include <iostream>
class N
{
public:
N() {}
N(int a) : _x(a) {}
virtual void print() {
std::cout << "N::_x = " << _x << std::endl;
}
int getX() { return _x; }
private:
int _x;
};
class F : public N
{
public:
F() {}
void print() {
std::cout << "F::getX() = " << getX() << std::endl;
}
};
class E
{
public:
E() {}
E(N &ti) : _myn(&ti) {}
void setMyn(const N &t) {
//_myn = &t;
}
void print() {
_myn->print();
}
private:
N *_myn;
};
int main(void)
{
F f();
E e;
//F &test = f;
e.setMyn(f);
e.print();
return 0;
}
------------------END OF CODE------------------
This code produce this error :
newsgroup.cpp:52: error: conversion invalide de " F (*)() " vers " int "
newsgroup.cpp:52: error: initialisation de l'argument 1 de " N::N(int) "
I can't find why it output an error there, any ideas ?
and I discovered a problem I don't understand. With the following code
(which has been shortened a bit for the ng), gcc output a strange error :
------------------CODE------------------
#include <iostream>
class N
{
public:
N() {}
N(int a) : _x(a) {}
virtual void print() {
std::cout << "N::_x = " << _x << std::endl;
}
int getX() { return _x; }
private:
int _x;
};
class F : public N
{
public:
F() {}
void print() {
std::cout << "F::getX() = " << getX() << std::endl;
}
};
class E
{
public:
E() {}
E(N &ti) : _myn(&ti) {}
void setMyn(const N &t) {
//_myn = &t;
}
void print() {
_myn->print();
}
private:
N *_myn;
};
int main(void)
{
F f();
E e;
//F &test = f;
e.setMyn(f);
e.print();
return 0;
}
------------------END OF CODE------------------
This code produce this error :
newsgroup.cpp: Dans function " int main() ":g++ -o newsgroup newsgroup.cpp
newsgroup.cpp:52: error: conversion invalide de " F (*)() " vers " int "
newsgroup.cpp:52: error: initialisation de l'argument 1 de " N::N(int) "
Exit code: 1
I can't find why it output an error there, any ideas ?