473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

inheritance problem (perhaps...)

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 ?

--
Alexandre
Jul 22 '05 #1
4 1025
Alexandre B wrote:
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 :
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 ?


See above.

Victor
Jul 22 '05 #2
Alexandre B <al********@cpm-fr.com> wrote in
news:41**********************@news.free.fr:
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.
E e;

//F &test = f;
e.setMyn(f);
e.print();

return 0;
}

Jul 22 '05 #3

"Alexandre B" <al********@cpm-fr.com> wrote in message news:41**********************@news.free.fr...
int main(void)

{
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());

Jul 22 '05 #4
thx all for your quick answers, sometimes staring at your own code
for hours just makes you blind, i guess.

--
Alexandre
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
0
by: Terry Hancock | last post by:
I've been discussing PyProtocols with a a friend collaborating with me on a SF game project, about the benefits and design concept of "component architecture", and I'm a little confused by what...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
11
by: Simon Woods | last post by:
Hi I have this recursive function and I want to walk the inheritance hierarchy to set field values .... the generic T is constrainted as the base class of the inheritance hierarchy Friend...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.