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

};

These are the errors I get:

(1) - FIeld has incomplete type 'Type'

(2) - No viable conversion from int to type (and some add 3. Passing argument to parameter here)

Could you please tell me what is it that I'm doing wrong?

Community
  • 1
  • 1
Teodora
  • 687
  • 2
  • 11
  • 21
  • That code makes no sense. `Type` is a type, not a variable, so you cannot assign a value to it. You meant to add a variable name in your constructor. Show us the definition of `Type`. – Ed S. Apr 12 '13 at 20:27
  • 1
    @EdS.: it's not assignment, it's initialization. – Cheers and hth. - Alf Apr 12 '13 at 20:28
  • @Teodora: it depends on the type you instantiate with. **show the code** – Cheers and hth. - Alf Apr 12 '13 at 20:30
  • I was given some headers and as homework I have to implement the functions. At fist instead of 'type' it was int. I have a base class Shape which has derived classes such as Triangle, Square etc. (everything worked perfectly until I started the last task which was to parametrize the classes so that Triangle would have int Points and so on. I'm new to templates, I read some articles and I thought that this is how I should do it.) – Teodora Apr 12 '13 at 20:32

1 Answers1

1

This code works for me. g++ 4.7.2 on Kubuntu 12.04.

By the way, do you have all the implementations of your Punct class in one file, i.e., the header file or you separate them into .h and .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;
}
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
  • Yes, I have implemented the not inline functions in the .cpp file, why? – Teodora Apr 12 '13 at 21:02
  • 1
    @Teodora For template class, usually all implementation goes in header file. If not, much efforts are needed. see this post: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – gongzhitaao Apr 12 '13 at 21:04
  • 1
    @Teodora: Templatized classes have to be fully defined (implemented) in header files. This restriction is imposed because of the way linkage works. Read [this question](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) for more information. – Alexander Shukaev Apr 12 '13 at 21:06