0
#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??

2
Contributors
1
Reply
13
Views
3 Years
Discussion Span
Last Post by tinstaafl
1

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;
}

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.