Operator overloading in template classes

This is a discussion on Operator overloading in template classes within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble with the friend functions being executed in this program - Can I get someone to look at ...

  1. #1
    moejams
    Guest

    Operator overloading in template classes

    I'm having trouble with the friend functions being executed in this program - Can I get someone to look at my code and help???

    Primitive.h
    Code:
    #ifndef PRIMITIVE_H
    #define PRIMITIVE_H
    
    #include <iostream>
    
    template <class T>
    class Primitive
    {
    
    	//friend istream& operator>>(istream& in, Primitive& p) { in >> p.data; return in; }
        //friend ostream& operator<<(ostream& out, const Primitive& p) { out << p.data; return out; }
      
    public:
    
    	Primitive(T num) { data=num; }
    	Primitive(Primitive& p) { data=p.getData(); }
    	~Primitive();
    
    
    	//assign
      const Primitive& operator=(const Primitive& p) { if(this!=p) data=p.getData(); return *this; } 
       
        //equality
      bool operator==(const Primitive& p) const { return !(data-p.getData()); }
    
        //addition
      Primitive operator+(const Primitive& p) const { return ( Primitive<T> temp(data+p.getData()) ); }
    
        //Unary + -- Turn to positive
      const Primitive& operator+(void) { if (data<0) data*=(-1); return *this; }
    
        //Unary - -- Turn to negative
      const Primitive& operator-(void) { if (data>0) data*=(-1); return *this; }  
    
    
        //set and get
      T getData() { return data;}
      void setData(T n) { data=n; }
    
    private:
      T data;
    
       
    };
    #endif

    main.cpp
    Code:
    //File main.cpp
    
    #include <iostream>
    #include "primitive.h"
    using std::cout;
    
    int main()
    {
      typedef Primitive<int> Pi;
      Pi obj1(1), obj2(2), obj3(obj1 + obj2);
      cout << obj3 << '\n';
      cout << ((obj1 == obj2) ? "equal" : "unequal") << '\n';
      cout << ((obj1 == obj1) ? "equal" : "unequal") << '\n';
    
    
      typedef Primitive<double> Pd;
      Pd obj4(34.639), obj5(obj4);
      obj4 = obj5;
      cout << +(-(obj4)) << '\n';
      return 0;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The friend functions need to be templated as well, because Primitive is not a complete type (as its missing a template parameter).

    i.e.
    Code:
    template<class T>
    friend istream& operator>>(istream& in, Primitive<T>& p)
    {
       in >> p.data;
       return in;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Check that... They don't have to be templated, but you must specify a complete type for the parameter. So,
    Code:
    friend istream& operator>>(istream& in, Primitive<int>& p) { ... }
    is valid as well, though it would probably defeat the purpose of having the function as a friend, and potentially cause some code bloat (or simply inability to use particular types).

    At any rate, when dealing with types, they must always be complete, and in the case of templated types, the template parameters are part of the type.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    moejams
    Guest
    I've done that, and here are the errors I'm getting:

    Code:
    --------------------Configuration: driver - Win32 Debug--------------------
    Compiling...
    driver.cpp
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2143: syntax error : missing ';' before '&'
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2433: 'istream' : 'friend' not permitted on data declarations
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2501: 'istream' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2244: 'istream' : unable to resolve function overload
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2061: syntax error : identifier 'istream'
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2501: '>>' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2805: binary 'operator >>' has too few parameters
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2333: '>>' : error in function declaration; skipping function body
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2143: syntax error : missing ';' before '&'
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2433: 'ostream' : 'friend' not permitted on data declarations
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2501: 'ostream' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2244: 'ostream' : unable to resolve function overload
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2061: syntax error : identifier 'ostream'
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2501: '<<' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2805: binary 'operator <<' has too few parameters
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2333: '<<' : error in function declaration; skipping function body
            c:\documents and settings\joe pipitone\desktop\primitive.h(44) : see reference to class template instantiation 'Primitive<T>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2143: syntax error : missing ';' before '&'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2433: 'istream' : 'friend' not permitted on data declarations
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2501: 'istream' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2244: 'istream' : unable to resolve function overload
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2061: syntax error : identifier 'istream'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2501: '>>' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2805: binary 'operator >>' has too few parameters
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2333: '>>' : error in function declaration; skipping function body
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2143: syntax error : missing ';' before '&'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2433: 'ostream' : 'friend' not permitted on data declarations
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2501: 'ostream' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2244: 'ostream' : unable to resolve function overload
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2061: syntax error : identifier 'ostream'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2501: '<<' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2805: binary 'operator <<' has too few parameters
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2333: '<<' : error in function declaration; skipping function body
            c:\documents and settings\joe pipitone\desktop\driver.cpp(11) : see reference to class template instantiation 'Primitive<int>' being compiled
    c:\documents and settings\joe pipitone\desktop\driver.cpp(12) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Primitive<int>' (or there is no acceptable conversion)
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2143: syntax error : missing ';' before '&'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2433: 'istream' : 'friend' not permitted on data declarations
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2501: 'istream' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2244: 'istream' : unable to resolve function overload
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2061: syntax error : identifier 'istream'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2501: '>>' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2805: binary 'operator >>' has too few parameters
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(10) : error C2333: '>>' : error in function declaration; skipping function body
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2143: syntax error : missing ';' before '&'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2433: 'ostream' : 'friend' not permitted on data declarations
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2501: 'ostream' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2244: 'ostream' : unable to resolve function overload
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2061: syntax error : identifier 'ostream'
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2501: '<<' : missing storage-class or type specifiers
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2805: binary 'operator <<' has too few parameters
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\primitive.h(11) : error C2333: '<<' : error in function declaration; skipping function body
            c:\documents and settings\joe pipitone\desktop\driver.cpp(18) : see reference to class template instantiation 'Primitive<double>' being compiled
    c:\documents and settings\joe pipitone\desktop\driver.cpp(20) : error C2678: binary '+' : no operator defined which takes a left-hand operand of type 'const class Primitive<double>' (or there is no acceptable conversion)
    Error executing cl.exe.
    
    driver.obj - 50 error(s), 0 warning(s)

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Check that top error line there... the one about the missing ';'

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Also make sure that the functions template parameter is its own parameter, and that it isn't trying to use the class's.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21