0

I am new to templates and am trying to compile this code:

template <typename _Type, typename _Comparator = std::equal_to<_Type> >
    class CSearch {
public:
    CSearch() : cmp(_Comparator()) {
    }

    CSearch(const _Comparator &_cmp) : cmp(_cmp) {
    }

    void Add(int id,
            const _Type & needle) {
        values.insert(needle); // problem
    }

    set<int> Search(const _Type & hayHeap) const {

    }
private:
    const _Comparator &cmp;

    /*typename(?)*/ set<const _Type&> values; // problem

    CSearch(const CSearch &other);
    CSearch &operator=(const CSearch &other);
};

(...)

int main(){
    CSearch <string> test;
}

I have done some searching and I suspect that the problem lies with the typename keyword. However, no matter how I try, I can't find the solution.

When there is the typename, I get expected nested-name-specifier error. When it is not, I get a freakishly long STL error.

What's the catch? Thanks


EDIT: how about this scenario, where I try to store pointers to objects in STLs?

template <typename _Type>
class Needle {
public:
    int ID;
    _Type *data;
};

template <typename _Type, typename _Comparator = std::equal_to<_Type> >
        class CSearch {
public:

    CSearch() : cmp(_Comparator()) {
    }

    CSearch(const _Comparator &_cmp) : cmp(_cmp) {

    }

    void Add(int id,
            const _Type & needle) {
        Needle<_Type> *tmp = new Needle<_Type>();
        tmp -> ID = id;
        tmp -> data = &needle;
        values.insert(tmp);
    }

    set<int> Search(const _Type & hayHeap) const {

    }
private:
    const _Comparator &cmp;

    set<const Needle*> values;

    CSearch(const CSearch &other);
    CSearch &operator=(const CSearch &other);
};
Martin Melka
  • 7,177
  • 16
  • 79
  • 138
  • 1
    possible duplicate of [Why does storing references (not pointers) in containers in C++ not work?](http://stackoverflow.com/questions/4010937/why-does-storing-references-not-pointers-in-containers-in-c-not-work) – Some programmer dude Apr 06 '13 at 12:02

3 Answers3

2

Firstly, anything like _Foo or _Bar is not for you to use, don't copy that habit from the implementations of the standard library. Also, it doesn't make things easier to read or write.

Now, the problem in your code is that you are trying to create a set of references, but references are not valid elements for containers. A "typename" is not needed there, because no dependent type is used there.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
1
set<const _Type & > values;

You have provided reference as an element type of set. Containers can not carry references they need pointers.

set<const _Type*> values;
  • @MartinMelka Needle is a template class. so it needs a template type when used. `set*> values;` –  Apr 06 '13 at 12:42
1
/*typename(?)*/ set<const _Type & > values; // problem
                               ^^^

because you cannot use reference as type in STL containers, use pointers instead:

set<const _Type*> values;
4pie0
  • 29,204
  • 9
  • 82
  • 118