inheritance problem (perhaps...)

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 :
g++ -o newsgroup newsgroup.cpp
newsgroup.cpp: Dans function " int main() ":
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 ?
 
A

Andre Kostur

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 :

[snip irrelevant code]
int main(void)
{
F f();

This would be declaring a function named 'f' which takes no arguments and
returns an object of type F by value. I don't think this is what you meant
to do... if you're trying to declare a variable named 'f' of the type F,
then get rid of the parentheses.
 
V

Victor Bazarov

Alexandre said:
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 :
[...]
int main(void)
{
F f();

This is a trap for beginners that don't read the FAQ. The statement
above is a declaration of a function 'f', not of an object 'f'. Lose
the parentheses.
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() ":
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 ?

See above.

Victor
 
R

Ron Natalie

Alexandre B said:
{
F f();
E e;
e.setMyn(f);

I'm assuming the error line is the one immediately above. It's always helpful
when posting to this group to indicate which line the error messages occur in
because it's hard to impossible to count the lines to figure it out.

Anyhow, the above like is a problem. You are calling setMyn with a pointer to
function. I suspect that either:

1. You wanted to declare f as a F value rather than a function, in which case you
should just have:
F f;
or
2. You want to pass the return value of the function f(), which would be
e.SetMyn(f());
 
A

Alexandre B

thx all for your quick answers, sometimes staring at your own code
for hours just makes you blind, i guess.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top