1

I am trying to implement a generic hashlist class using templates and I am trying to inherit from the base class but getting lots of compile errors. Here is my code:

#ifndef BASEHASHLIST_H_
#define BASEHASHLIST_H_

#include <string>
#include <boost/unordered_set.hpp>
#include <iostream>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>



    template <typename T>
    class BaseHashList
    {

    private:
        boost::interprocess::interprocess_semaphore m_semaphore;

    protected:

        boost::unordered_set<T> m_hsHashSet;                
        typename boost::unordered_set<T>::iterator m_hsIter;            
    public:

        BaseHashList();
    };

    template <typename T>
    BaseHashList<T>::BaseHashList():m_semaphore(1){}

#endif /* BASEHASHLIST_H_ */

And here is the class that is inheriting from the base class:

#ifndef ACCOUNTLIST_H_
#define ACCOUNTLIST_H_

#include "BaseHashList.h"

    class AccountList : public BaseHashList<unsigned long>
    {
    public:
        AccountList(std::string p_strFile, unsigned long p_ulMaxAccountNo);
        ~AccountList(void);

        int m_iVersion;
        std::string m_strFilePath;


    private:
        unsigned long m_ulMaxAccountNo;
    };


#endif /* ACCOUNTLIST_H_ */

and here is the cpp file:

#include "AccountList.h"

AccountList::AccountList(std::string p_strFile, unsigned long p_ulMaxAccountNo)
: BaseHashList<unsigned long>::m_hsHashSet<unsigned long>(),
  m_iVersion(0),
  m_strFilePath(p_strFile)
{
    m_ulMaxAccountNo = p_ulMaxAccountNo;
}


AccountList::~AccountList(){}

I am receiving a lot of compile time errors such as:

expected template-name before token '<' expected '(' before token '<'

For such a simple task I spent couple of hours and I am super frustrated, does anybody see what I am doing wrong here?

2 Answers 2

2

This initaliser in AccountList's constructor looks wrong to me:

BaseHashList<unsigned long>::m_hsHashSet<unsigned long>()

You should initalise the members of BaseHashList inside a constructor of BaseHashList itself, one will always either explicitly or implicitly be called.

This example is simplified and similarly wrong:

struct A {
  int bar;
};

struct B : A {
  B() : A::bar(0) {}
};

(saying bar(0) would also be wrong there)

However you can get the desired behaviour:

struct A {
  A() : bar(0) {}
  int bar;
};

struct B : A {
  B() {} // Implicitly calls A::A although we could have explicitly called it
};

The constructor of A gets called and given a chance to initalise its members here still.

Sign up to request clarification or add additional context in comments.

Comments

0

When you inherit from a template class, you have to add the template instruction to the child class too :

template <typename T>
class A : public B<T>

You also have to add the template instruction before the definition of constructors and methods :

template <typename T>
A<T>::A() : B<T>() {...}

template <typename T>
A<T>::~A() {...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.