abc
abc

Reputation: 2027

error: unkown type name "my_class"

So I declared a template class called my_class and I use this class in another file, "main.cpp".

I included "my_class.h" in my main.cpp and I'm getting unknown type name error, anyone know what's going on?

Here is some code:

// "my_class.h"
#ifndef TYPE_H_DEFINE
#define TYPE_H_DEFINE

#include <vector>
#include <algorithm>

namespace wtvr {

template<class T>
class my_class {
public:
    void add(const T&);
    const T& get();
private:
    std::vector<T> my_class_vec; 
};

template<class T>
void my_class<T>::add(const T& obj) {
    my_class_vec.push_back(obj);
}

template<class T>
const T& my_class<T>::get() {
    std::random_shuffle(my_class_vec.begin(), my_class_vec.end());
    return my_class_vec[0];
}
}

#endif 

Upvotes: 0

Views: 99

Answers (1)

abc
abc

Reputation: 2027

It was a tricky one!

Turns out that my include guard names

#ifndef TYPE_H_DEFINE
#define TYPE_H_DEFINE

was already used by the standard c++ library so I changed it to a different name, no wonder why Bjarne Stroustrup hates macros!! Thank you everybody!

Upvotes: 1

Related Questions