Inheritance problem

Hi,

I am trying to create a simple class that is derived from a templated base class but I keep on getting an error that states "error: connection was not declared in this scope". I will try and show some code below:

Subscriber.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace a
{

template <class T>
class Subscriber 
{
protected:
    Connection &m_connection;

public:
	
    Subscriber(Connection &connection, const std::string &topic)
		: m_connection(connection)
    {
    }

    virtual ~Subscriber()
    {
    }

    //.... some more generic methods here
};
}


InitialLoadSub.h:
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
29
30
31
32
33

#include "Subscriber.h"

namespace b
{              
    template <class T>
    class InitialSubscriber : public Subscriber<T>
    {
    public:
            
        InitialSubscriber(Connection &connection, 
                        const std::string &topic, const std::string & iTopic, const std::string & iName )        
            : Subscriber<T>(connection, topic),
              topic_(topic), iSubscriber_(0), iTopic_(iTopic), iName_(iName), iResponseStarted_(false)
        {
            m_connection.RegisterEventSink(this);
        }
            
        virtual ~InitialSubscriber()
        {
            m_connection.UnregisterEventSink(this);
            delete iSubscriber_;
        }
    
    private:
        std::string topic_;
        EMsgSubscriber *iSubscriber_;
        std::string iTopic_;
        std::string iName_;
        bool iResponseStarted_;
    };
}


Up to this point everything compiles fine. However, when I provide the specialisation below I get an error that says that "m_connection was not declared in this scope" referring to the line in InitialSubscriber.h 's constructor.

Finally I provide the calss where I include the specialisation:
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
29
30
31
32
33
34
#include "InitialLoadSub.h"
namespace b
{
    class Specialised : public InitialSubscriber<Control> >
    {
    public:
        Specialised (Connection &connection, 
                        const std::string &topic, const std::string & iTopic, const std::string & iName);
        virtual ~Specialised();
        
    protected:
        
    };
}


/// and below the source file///////////

#include "Specialised .h"

namespace b
{
    Specialised::Specialised(Connection &connection, 
                        const std::string &topic, const std::string & iTopic, const std::string & iName)
          : InitialSubscriber<Control> >(connection, topic, iTopic, iName)
    {
        
    }
    
    Specialised::~Specialised()
    {        
    }

}


I'm assuming it's something obvious that I'm doing wrong here... any suggestions would be very welcome. This is a cut down verion of my class but I believe all the necessary bits are there.

Regards
Last edited on
I don't know if this is the cause of the problem, but you need to close your class definitions with a semicolon:

1
2
3
4
5
6
class A
{

  // Stuff

};  // <------ SEMICOLON 


You've done this for Specialised, but not for Subscriber or InitialSubscriber.
Hi ,

thanks for the response. I had indeed closed the definitions with a semicolon so the problem still persists.

I have managed to get the files to compile by specialising the type in InitialSubscriber.h as follows:

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 b
{              
    class InitialSubscriber : public Subscriber<Control>
    {
    public:
            
        InitialSubscriber(Connection &connection, 
                        const std::string &topic, const std::string & iTopic, const std::string & iName )        
            : Subscriber<Control>(connection, topic),
              topic_(topic), iSubscriber_(0), iTopic_(iTopic), iName_(iName), iResponseStarted_(false)
        {
            m_connection.RegisterEventSink(this);
        }
            
        virtual ~InitialSubscriber()
        {
            m_connection.UnregisterEventSink(this);
            delete iSubscriber_;
        }
    
    private:
        std::string topic_;
        EMsgSubscriber *iSubscriber_;
        std::string iTopic_;
        std::string iName_;
        bool iResponseStarted_;
    };
}


However, this is not the ideal generic behaviour I am after.

Regards
You've included Subscriber.h header but class Subscriber is in namespace "a" so it is invisible, try adding this line and compile again

1
2
#include "Subscriber.h"
using namespace a;  // content of 'a' will be visible 
Last edited on
Topic archived. No new replies allowed.