473,497 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A problem with inheritance

Hello again and thnx to everybody for previous feedbacks.
This code works quite nicely when I omit the inheritance in
MyContainer.h

---------------
MyClass.h
---------------

#pragma once

#include "MyContainer.h"

class MyClass {
private:
int myInteger;
MyContainer memberContainer;
public:
MyClass(void);
~MyClass(void);

};

---------------------
MyContainer.h
---------------------

#pragma once

#include <vector>

using namespace std;

class MyClass; // forward declaration

class MyContainer : BaseContainer<MyClass *{
private:
vector<MyClass *myVector;
MyClass *pointer;
public:
MyContainer(void);
int add(const MyClass *element) { return 1; }
~MyContainer(void);

};

------------------------
BaseContainer.h
------------------------

#pragma once

template <class Tip>
class BaseContainer
{
public:
BaseContainer(void);
virtual int add(const Tip element) = 0;
~BaseContainer(void);
};
-------------
main.cpp
-------------

#include <iostream>

#include "MyClass.h"
#include "MyContainer.h"

void main() {
MyClass myInstance;
MyContainer myContainerInstance;

}

However, with BaseContainer inherited to MyContainer, the following
problem occurs:

f:\C++\Projects\Probarka\MyClass.h(9) : error C2259: 'MyContainer' :
cannot instantiate abstract class
due to following members:
'int BaseContainer<Tip>::add(const Tip)' : pure virtual
function was not defined
with
[
Tip=MyClass *
]
f:\C++\Projects\Probarka\BaseContainer.h(8) : see declaration
of 'BaseContainer<Tip>::add'
with
[
Tip=MyClass *
]
I can't understand what's wrong. Without the inheritance it works
perfectly. Please help.

Aug 7 '06 #1
14 1697
ax****@gmail.com wrote:
int add(const MyClass *element) { return 1; }
does not have override
virtual int add(const Tip element) = 0;
when Tip is MyClass*.

If Tip is Myclass*, then const Tip is:

MyClass* const

hence the overrider would be
int add(MyClass* const element) { ..
Aug 7 '06 #2

Ron Natalie je napisao/la:
ax****@gmail.com wrote:
int add(const MyClass *element) { return 1; }

does not have override
virtual int add(const Tip element) = 0;

when Tip is MyClass*.

If Tip is Myclass*, then const Tip is:

MyClass* const

hence the overrider would be
int add(MyClass* const element) { ..
I tried changing the 'add' method to what you've said, but I'm still
getting the same error message.

Aug 7 '06 #3
ax****@gmail.com wrote:
Hello again and thnx to everybody for previous feedbacks.
This code works quite nicely when I omit the inheritance in
MyContainer.h

---------------
MyClass.h
[snip]
MyContainer.h
---------------------

#pragma once

#include <vector>

using namespace std;

class MyClass; // forward declaration

class MyContainer : BaseContainer<MyClass *{
private:
vector<MyClass *myVector;
MyClass *pointer;
public:
MyContainer(void);
int add(const MyClass *element) { return 1; }
~MyContainer(void);

};
[snip]
-------------
main.cpp
-------------

#include <iostream>

#include "MyClass.h"
#include "MyContainer.h"

void main() {
MyClass myInstance;
MyContainer myContainerInstance;

}
[snip]

Ron Natalie has already answered your question. Two comments on your
code, though.

First, please don't put "#include namespace std" in a header file.
It's a bad habit and will come back to bite you - or one of your
colleagues - some day.

Second, don't use "void main" - it's not standard, and probably won't
work if you switch to a different compiler.

Best regards,

Tom

Aug 7 '06 #4

Thomas Tutone je napisao/la:
ax****@gmail.com wrote:
Hello again and thnx to everybody for previous feedbacks.
This code works quite nicely when I omit the inheritance in
MyContainer.h

---------------
MyClass.h

[snip]
MyContainer.h
---------------------

#pragma once

#include <vector>

using namespace std;

class MyClass; // forward declaration

class MyContainer : BaseContainer<MyClass *{
private:
vector<MyClass *myVector;
MyClass *pointer;
public:
MyContainer(void);
int add(const MyClass *element) { return 1; }
~MyContainer(void);

};

[snip]
-------------
main.cpp
-------------

#include <iostream>

#include "MyClass.h"
#include "MyContainer.h"

void main() {
MyClass myInstance;
MyContainer myContainerInstance;

}

[snip]

Ron Natalie has already answered your question. Two comments on your
code, though.

First, please don't put "#include namespace std" in a header file.
It's a bad habit and will come back to bite you - or one of your
colleagues - some day.

Second, don't use "void main" - it's not standard, and probably won't
work if you switch to a different compiler.

Best regards,

Tom

Thnx Tom, you're right, I shouldn't use void main, but I usually use
WinMain anyway. As for the using namespace directive, you're right,
I'll remove it.
If I understood correctly, Ron Natalie said that I should change
int add(const MyClass *element) { return 1; };
to:
int add(MyClass const *element) { return 1; };

I tried doing this, but with no effect.

Aug 7 '06 #5

axe...@gmail.com wrote:
Thnx Tom, you're right, I shouldn't use void main, but I usually use
WinMain anyway. As for the using namespace directive, you're right,
I'll remove it.
If I understood correctly, Ron Natalie said that I should change
int add(const MyClass *element) { return 1; };
to:
int add(MyClass const *element) { return 1; };
No, he said change it to:

int add(MyClass* const element) { return 1; }

(Lose that ";" after the closing bracket of the function definition, by
the way.)

Best regards,

Tom

Aug 7 '06 #6
I'm getting a different error message, though...
MyContainer.obj : error LNK2019: unresolved external symbol "public:
__thiscall BaseContainer<class MyClass *>::~BaseContainer<class MyClass
*>(void)" (??1?$BaseContainer@PAVMyClass@@@@QAE@XZ) referenced in
function __unwindfunclet$??0MyContainer@@QAE@XZ$0
MyContainer.obj : error LNK2019: unresolved external symbol "public:
__thiscall BaseContainer<class MyClass *>::BaseContainer<class MyClass
*>(void)" (??0?$BaseContainer@PAVMyClass@@@@QAE@XZ) referenced in
function "public: __thiscall MyContainer::MyContainer(void)"
(??0MyContainer@@QAE@XZ)

What does this mean? I provided definitions for the constructor and the
destructor in BaseContainer.cpp

Aug 7 '06 #7
ps Thomas: I changed it to what Ron said.

Aug 7 '06 #8
Strange, this reports the previous error message in the project I've
posted, but in my original project everything is working neatly.

Ron, Thomas - Thnx for your feedback

Bye

Aug 7 '06 #9

ax****@gmail.com wrote:
I'm getting a different error message, though...
MyContainer.obj : error LNK2019: unresolved external symbol "public:
__thiscall BaseContainer<class MyClass *>::~BaseContainer<class MyClass
*>(void)" (??1?$BaseContainer@PAVMyClass@@@@QAE@XZ) referenced in
function __unwindfunclet$??0MyContainer@@QAE@XZ$0
MyContainer.obj : error LNK2019: unresolved external symbol "public:
__thiscall BaseContainer<class MyClass *>::BaseContainer<class MyClass
*>(void)" (??0?$BaseContainer@PAVMyClass@@@@QAE@XZ) referenced in
function "public: __thiscall MyContainer::MyContainer(void)"
(??0MyContainer@@QAE@XZ)

What does this mean?
It means you didn't provide definitions for the constructor and
destructor for template<class BaseContainer.
>I provided definitions for the constructor and the
destructor in BaseContainer.cpp
You didn't post BaseContainer.cpp, but the solution to your problem is
in the FAQ (sections 35.12 and following):

http://www.parashift.com/c++-faq-lit...html#faq-35.12

Best regards,

Tom

Aug 7 '06 #10

Thomas Tutone je napisao/la:
ax****@gmail.com wrote:
I'm getting a different error message, though...
MyContainer.obj : error LNK2019: unresolved external symbol "public:
__thiscall BaseContainer<class MyClass *>::~BaseContainer<class MyClass
*>(void)" (??1?$BaseContainer@PAVMyClass@@@@QAE@XZ) referenced in
function __unwindfunclet$??0MyContainer@@QAE@XZ$0
MyContainer.obj : error LNK2019: unresolved external symbol "public:
__thiscall BaseContainer<class MyClass *>::BaseContainer<class MyClass
*>(void)" (??0?$BaseContainer@PAVMyClass@@@@QAE@XZ) referenced in
function "public: __thiscall MyContainer::MyContainer(void)"
(??0MyContainer@@QAE@XZ)

What does this mean?

It means you didn't provide definitions for the constructor and
destructor for template<class BaseContainer.
I provided definitions for the constructor and the
destructor in BaseContainer.cpp

You didn't post BaseContainer.cpp, but the solution to your problem is
in the FAQ (sections 35.12 and following):

http://www.parashift.com/c++-faq-lit...html#faq-35.12

Best regards,

Tom

Thnx!

Aug 7 '06 #11
ax****@gmail.com wrote:
\

right, I shouldn't use void main, but I usually use
WinMain anyway. As for the using namespace directive, you're right,
I'll remove it.
If I understood correctly, Ron Natalie said that I should change
int add(const MyClass *element) { return 1; };
to:
int add(MyClass const *element) { return 1; };

I tried doing this, but with no effect.
No you don't understand me. The above two are IDENTICAL
and NOT what I wrote.

int addMyclass * const element)
is what I wrote and I meant it.
Aug 8 '06 #12
In article <44***********************@news.newshosting.com> ,
ro*@spamcop.net says...

[ ... ]
int addMyclass * const element)
is what I wrote and I meant it.
Well, I'm pretty sure what you meant (and wrote) was:

int add(Myclass * const element)

but what's a missing parenthesis between friends?
[I suppose the answer to that would depend on whether you classify the
compiler as a friend. :-) ]

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 8 '06 #13

Ron Natalie je napisao/la:
ax****@gmail.com wrote:
\

right, I shouldn't use void main, but I usually use
WinMain anyway. As for the using namespace directive, you're right,
I'll remove it.
If I understood correctly, Ron Natalie said that I should change
int add(const MyClass *element) { return 1; };
to:
int add(MyClass const *element) { return 1; };

I tried doing this, but with no effect.
No you don't understand me. The above two are IDENTICAL
and NOT what I wrote.

int addMyclass * const element)
is what I wrote and I meant it.
Thnx, got it. It works now.

Aug 8 '06 #14
Thomas Tutone wrote:
ax****@gmail.com wrote:
>Hello again and thnx to everybody for previous feedbacks.
This code works quite nicely when I omit the inheritance in
MyContainer.h

---------------
MyClass.h

[snip]
>MyContainer.h
---------------------

#pragma once

#include <vector>

using namespace std;

class MyClass; // forward declaration

class MyContainer : BaseContainer<MyClass *{
private:
vector<MyClass *myVector;
MyClass *pointer;
public:
MyContainer(void);
int add(const MyClass *element) { return 1; }
~MyContainer(void);

};

[snip]
>-------------
main.cpp
-------------

#include <iostream>

#include "MyClass.h"
#include "MyContainer.h"

void main() {
MyClass myInstance;
MyContainer myContainerInstance;

}

[snip]

Ron Natalie has already answered your question. Two comments on your
code, though.

First, please don't put "#include namespace std" in a header file.
It's a bad habit and will come back to bite you - or one of your
colleagues - some day.

Second, don't use "void main" - it's not standard, and probably won't
work if you switch to a different compiler.
Third, '#pragma once' is also not standard and should be guarded by
#ifdef's identifying your specific compiler (other compilers will need
the usual include guards of course).

Aug 8 '06 #15

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

Similar topics

14
6377
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
2
4318
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
20
10026
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...
4
1433
by: Sebastian Böck | last post by:
Hello all, i have a view defined as a simple select of a table. This table is inherited by a couple of others. All entries belong to the child-tables. I also have an unconditional update rule...
2
6294
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
60
4862
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...
5
2723
by: colint | last post by:
Hi I'm fairly new to c++ and I have a question regarding inheritance. I'm trying to create a class based on 2 inherited classes, e.g. class A { ... } class B: public A
3
2532
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
21
2451
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
6
5703
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.