字段类型不完整

时间:2013-04-12 20:25:09

标签: c++ templates

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) - FIeld的类型不完整&#39; Type&#39;

     

(2) - 没有可行的从int到type的转换(有些添加3.将参数传递给参数)

你能否告诉我,我做错了什么?

1 个答案:

答案 0 :(得分:1)

此代码适用于我。 g++ 4.7.2上的Kubuntu 12.04

顺便说一下,您是否将Punct类的所有实现都放在一个文件中,即头文件中,或者将它们分为.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;
}