A problem with inheritance

A

axel22

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.
 
R

Ron Natalie

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) { ..
 
A

axel22

Ron Natalie je napisao/la:
does not have override


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.
 
T

Thomas Tutone

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
 
A

axel22

Thomas Tutone je napisao/la:
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.
 
T

Thomas Tutone

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
 
A

axel22

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
 
A

axel22

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
 
T

Thomas Tutone

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 said:
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-lite/templates.html#faq-35.12

Best regards,

Tom
 
R

Ron Natalie

(e-mail address removed) 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.
 
J

Jerry Coffin

[ ... ]
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. :) ]
 
A

axel22

Ron Natalie je napisao/la:
(e-mail address removed) wrote:
\

right, I shouldn't use void main, but I usually use
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.
 
M

Markus Schoder

Thomas said:
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).
 

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

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,099
Latest member
AmbrosePri
Top