0

I have the following header file, tag T is a struct, don't worry about it

template <tag T>
class index_class {
public:
    index_class();
    index_class(const index_class& other);
    explicit index_class(index_carrier_type<T> value);

    index_class& operator=(const index_class& other);

    index_carrier_type<T> value() const;

    bool operator==(index_class& other);

    bool operator!=(index_class& other);

    friend std::ostream & operator<<(std::ostream& os, const index_class& idx);

private:
    index_carrier_type<T> value_;
};

And in the .cpp file I have the following:

#include "tagged_vector.hpp"

template <tag T>
std::ostream& index_class<T>::operator<<(std::ostream& os, const index_class& idx) {
    if (idx.value_ == std::numeric_limits<index_carrier_type<T>>::max()) {
        throw std::range_error("Uninitialized index");
    }
    os << idx.value_;
    return os;
}

Yet I get an error. I have e problem with the friend operator<<, but I don't know what. I am using c++20.

I tried to remove the friend keyword.

273K
  • 29,503
  • 10
  • 41
  • 64
Norbi
  • 67
  • 1
  • 6
  • Templates are different from non-templated code. You need to implement templates in the header instead of a cpp file in most cases. The linked duplicate explains the reason and shows a possible workaround if you can limit the types that the template are used with and instantiate them in a cpp file with explicit instantiation. Related to explicit instantiation: [https://stackoverflow.com/questions/2351148/explicit-template-instantiation-when-is-it-used](https://stackoverflow.com/questions/2351148/explicit-template-instantiation-when-is-it-used) – drescherjm Apr 03 '23 at 16:33
  • 1
    *`Yet I get an error.* What error do you get? We can't read what you see on your screen. – 273K Apr 03 '23 at 16:40

0 Answers0