1
template <class Type>
class Punct {
    
protected:
    
    Type _x; // (1)
    Type _y; // (1)
    
public:

    Punct(Type = 0, Type = 0); // (2)
    ~Punct();
    inline Type getX() const { return _x; }
    inline Type getY() const { return _y; }
  
    inline void setX(Type x) { _x = x; }
    inline void setY(Type y) { _y = y; }
    inline void moveBy(int x, int y) { _x = x; _y = y; }

friend std::istream &operator>>(std::istream&ins, Punct<Type>& A);
friend std::ostream &operator<<(std::ostream&outs, const Punct<Type>& A);

};

これらは私が得るエラーです:

(1) - フィールドに不完全な型 'Type' があります

(2) - int から型への実行可能な変換はありません (一部は 3 を追加します。ここで引数をパラメーターに渡します)

私が間違っていることを教えてください。

4

1 に答える 1

1

このコードは私にとってはうまくいきます。g++ 4.7.2オンKubuntu 12.04

ところで、Punctクラスのすべての実装を 1 つのファイル、つまりヘッダー ファイルにまとめていますか、それとも と に分けています.h.cpp?

#include <iostream>
using namespace std;

template <class Type>
class Punct {

protected:

    Type _x; // (1)
    Type _y; // (1)

public:

    Punct(Type = 0, Type = 0) {}; // (2) <- empty function body added
    ~Punct() {}; // <- empty function body added
    inline Type getX() const { return _x; }
    inline Type getY() const { return _y; }

    inline void setX(Type x) { _x = x; }
    inline void setY(Type y) { _y = y; }
    inline void moveBy(int x, int y) { _x = x; _y = y; }

    template<class T> // <- added
    friend std::istream &operator>>(std::istream&ins, Punct<T>& A);
    template<class T> // <- added
    friend std::ostream &operator<<(std::ostream&outs, const Punct<Type>& A);

};

// bogus function added
template<class T>
istream &operator>> (istream &i, Punct<T> &a)
{
    return i;
}

// bogus function added
template<typename T>
ostream &operator<< (ostream &i, const Punct<T>& a)
{
    return i;
}

int main()
{
    Punct<int> a;
}
于 2013-04-12T20:42:49.927 に答える