vector and nested classes

Hi,

I am using <vector> with an interface like,

vector<interface*> b;


and then filling the container as follows,

b.push_back(new derived_class(x,y);

Both of these work fine, however, i am then trying to use part of a nested class, such as,

b.push_back(new X::Y(i,j));

This doesn't compile. I am at a loss as how to get round this problem. Any advice would be awesome.

Cheers

(Also apologies for any miss-use of terminology. Still new to programming and this is my first attempt at using the stl)

Is the nested class (Y) derived from interface too? Is it declared in the global scope of the class containing it (X)? What compilation errors do you get exactly?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

namespace N
{

class interface
{public:
    pure virtual functions
};

class X: public interface
{
    public:
         class Y
         {
           stuff related to Y
         };

         another nested class
        {
        };

};

class derived_class:public shape
{
};

};



The error i get is;

error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'N::X::Y *' to 'N::X *const &'

with
[
_Ty= N :: interface*
]

Reason: cannot convert from 'N::X::Y *' to 'N::interface *const '

Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
As you can see, class Y isn't derived from interface. I think this is the cause of the problem. Try defining class Y like this:

1
2
3
4
5
6
7
8
9
10
class X: public interface
{
    public:
         class Y: public interface //<-add this here!
         {
           //stuff related to Y
         };

     //...
};


But if it doesn't make sense that Y should be derived from interface then don't do it... Find another way to solve it, redesigning your inter-class relationships. Why do you need the nested classes anyway? What are you trying to do?
Last edited on
Hi, Sorry for late reply, have been without internet for a few days but didn't want to appear rude. I changed the structure of my code so everthing works as intended now.

Thanks for your help
Topic archived. No new replies allowed.