#include <iostream>

template <unsigned, unsigned> class Screen;
template <unsigned H, unsigned W>
    std::ostream& operator<<(std::ostream&, 
                             const Screen<H, W>&);
template <unsigned H, unsigned W>
    std::istream& operator>>(std::istream&,
                             const Screen<H, W>&);

template <unsigned H, unsigned W>
class Screen {

    friend std::ostream& operator<<<H, W>
           (std::ostream&, const Screen<H, W>&);
    friend std::istream& operator>><H, W>
           (std::istream&, const Screen<H, W>&);

public:
    Screen(): height(0), width(0) { }

private:
    std::size_t height, width;
};

template <unsigned H, unsigned W>
std::ostream& operator<<(std::ostream& os, const Screen<H, W>& val)
{
    os << val.height << " " << val.width;
    return os;
}

template <unsigned H, unsigned W>
std::istream& operator>>(std::istream& is, const Screen<H, W>& val)
{
    is >> val.height >> val.width;
    return is;
}

int main()
{
    Screen<50, 20> derp;
    std::cin >> derp;
    std::cout << derp;

    return 0;
}

So, I get this error from the code above,

ERROR IMAGE

there was no problem if the "std::cin >> derp;" line is removed, it means it could work with std::cout, but why not std::cin??

The main problem appears to be int trying to change the value of a constant. If you remove the const declaration for the >> operator in the declaration and the method, it should work:

#include <iostream>
template <unsigned, unsigned> class Screen;
template <unsigned H, unsigned W>
std::ostream& operator<<(std::ostream&,
                         const Screen<H, W>&);
template <unsigned H, unsigned W>
std::istream& operator>>(std::istream&,
                         Screen<H, W>&);
template <unsigned H, unsigned W>
class Screen {
    friend std::ostream& operator<<<H, W>
        (std::ostream&, const Screen<H, W>&);
    friend std::istream& operator>><H, W>
        (std::istream&, Screen<H, W>&);
public:
    Screen(): height(0), width(0) { }
private:
    std::size_t height, width;
};
template <unsigned H, unsigned W>
std::ostream& operator<<(std::ostream& os, const Screen<H, W>& val)
{
    os << val.height << " " << val.width;
    return os;
}
template <unsigned H, unsigned W>
std::istream& operator>>(std::istream& is, Screen<H, W>& val)
{
    is >> val.height >> val.width;
    return is;
}
int main()
{
    Screen<50, 20> derp;
    std::cin >> derp;
    std::cout << derp;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.