Thread: typename madness

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    typename madness

    Im trying to make a class that can hold of the position of something(its x,y,z coords).
    I want it to be able to work with any kind of number so i thought id be easy with some typenaming templates for my fuctions and the private variables.

    i keep getting:
    data member ‘x’ cannot be a member template
    data member ‘y’ cannot be a member template
    data member ‘z’ cannot be a member template
    Im guessing im going about this wrong. Heres the code id like to make more generic. Any advice?

    Code:
    #ifndef __POSITION_H_
    #define __POSITION_H_
    
    #include <iostream>
    using namespace std;
    
    class position {
    	public:
    		position(int X=0, int Y=0, int Z=0) {
    			set(X, Y, Z);
    		}
    
    		//
    		// Set position
    		//
    
    		void setX(int X) {
    			x = X;
    		}
    
    		void setY(int Y) {
    			y = Y;
    		}
    
    		void setZ(int Z) {
    			z = Z;
    		}
    
    		void set(int X, int Y, int Z) {
    			setX(X);
    			setY(Y);
    			setZ(Z);
    		}
    
    		//
    		// Get position
    		//
    
    		int getX() {
    			return x;
    		}
    
    		int getY() {
    			return y;
    		}
    
    		int getZ() {
    			return z;
    		}
    
    	private:
    		int x;
    		int y;
    		int z;
    };
    
    #endif
    Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Do exactly what you probably were doing. Except don't intialize your "template" variables with integers in your default constructor. I'm not sure, but I'd guess that'd be a problem with templates on some compilers.

    Code:
    template <class Type>
    class position {
    	public:
    		position(Type X, Type Y, Type Z) {
    			set(X, Y, Z);
    		}
    
    		//
    		// Set position
    		//
    
    		void setX(Type X) {
    			x = X;
    		}
    
    		void setY(Type Y) {
    			y = Y;
    		}
    
    		void setZ(Type Z) {
    			z = Z;
    		}
    
    		void set(Type X, Type Y, Type Z) {
    			setX(X);
    			setY(Y);
    			setZ(Z);
    		}
    
    		//
    		// Get position
    		//
    
    		Type getX() {
    			return x;
    		}
    
    		Type getY() {
    			return y;
    		}
    
    		Type getZ() {
    			return z;
    		}
    
    	private:
    		Type x;
    		Type y;
    		Type z;
    };
    Last edited by SlyMaelstrom; 05-13-2006 at 05:14 PM.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This should do for starters.....

    Code:
    template<class T>
    class position {
    	public:
    		position(const T &X=0, const T &Y=0, const T &Z=0): x(X), y(Y), z(Z) {}
    
    		//
    		// Set position
    		//
    
    		void setX(const T &X) {
    			x = X;
    		}
    
    		void setY(const T &Y) {
    			y = Y;
    		}
    
    		void setZ(const T &Z) {
    			z = Z;
    		}
    
    		void set(const T &X, const T &Y, const T &Z) {
    			setX(X);
    			setY(Y);
    			setZ(Z);
    		}
    
    		//
    		// Get position
    		//
    
    		T getX() const {
    			return x;
    		}
    
    		T getY() const {
    			return y;
    		}
    
    		T getZ() const {
    			return z;
    		}
    
    	private:
    		T x;
    		T y;
    		T z;
    };
    Notable changes (apart from making this a template class) are that I'm passing arguments by const reference and the getters are now const methods. The constructor also initialises the members directly rather than (as in your code) initialising and then setting them.

    This assumes that type T has the following properties;
    1) It can be assigned a value of zero
    2) It has an assignment operator and copy constructor

    As an aside, it would be better to use a different include guard: names starting with two underscores are reserved for use by the implementation (eg there is a potential of them clashing with names used in the standard library).

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    57
    Hate to ask a dumb follow up here but doesnt look like i have much of a choice. Im drawing a blank for some reason.

    position.cpp:7: error: redefinition of ‘position<Type>::position()’
    position.h:10: error: ‘position<Type>::position()’ previously declared here
    position.cpp:12: error: redefinition of ‘position<Type>::position(Type, Type)’
    position.h:14: error: ‘position<Type>::position(Type, Type)’ previously declared here
    position.cpp:17: error: redefinition of ‘position<Type>::position(Type, Type, Type)’
    position.h:18: error: ‘position<Type>::position(Type, Type, Type)’ previously declared here
    position.cpp:25: error: redefinition of ‘void position<Type>::setX(Type)’
    position.h:26: error: ‘void position<Type>::setX(Type)’ previously declared here
    position.cpp:30: error: redefinition of ‘void position<Type>::setY(Type)’
    position.h:30: error: ‘void position<Type>::setY(Type)’ previously declared here
    position.cpp:35: error: redefinition of ‘void position<Type>::setZ(Type)’
    position.h:34: error: ‘void position<Type>::setZ(Type)’ previously declared here
    position.cpp:40: error: prototype for ‘void position<Type>::set(Type, Type)’ does not match any in class ‘position<Type>’
    position.h:38: error: candidate is: void position<Type>::set(Type, Type, Type)
    position.cpp:40: error: template definition of non-template ‘void position<Type>::set(Type, Type)’
    position.cpp:47: error: redefinition of ‘void position<Type>::set(Type, Type, Type)’
    position.h:38: error: ‘void position<Type>::set(Type, Type, Type)’ previously declared here
    position.cpp:57: error: redefinition of ‘Type position<Type>::getX()’
    position.h:48: error: ‘Type position<Type>::getX()’ previously declared here
    position.cpp:62: error: redefinition of ‘Type position<Type>::getY()’
    position.h:52: error: ‘Type position<Type>::getY()’ previously declared here
    position.cpp:67: error: redefinition of ‘Type position<Type>::getZ()’
    position.h:56: error: ‘Type position<Type>::getZ()’ previously declared here
    Code:
    #ifndef _POSITION_H_
    #define _POSITION_H_
    
    template <class Type>
    class position {
    	public:
    		//
    		// Constructors
    		//
    		position();				// Default
    		position(Type X, Type Y);		// 2d
    		position(Type X, Type Y, Type Z);	// 3d
    			
    		//
    		// Set position
    		//
    		void setX(Type X);
    		void setY(Type Y);
    		void setZ(Type Z);
    
    		void set(Type X, Type Y);		// 2d
    		void set(Type X, Type Y, Type Z);	// 3d
    
    		//
    		// Get position
    		//
    		Type getX();
    		Type getY();
    		Type getZ();
    
    	private:
    		Type x;
    		Type y;
    		Type z;
    };
    
    #endif
    Code:
    #include "position.h"
    
    //
    // Constructors
    //
    template <class Type>
    position<Type>::position() {// Default
    	;;
    }
    
    template <class Type>
    position<Type>::position(Type X, Type Y) {// 2d
    	set(X, Y, 0);
    }
    
    template <class Type>
    position<Type>::position(Type X, Type Y, Type Z) {	// 3d
    	set(X, Y, Z);
    }
    
    //
    // Set position
    //
    template <class Type>
    void position<Type>::setX(Type X) {
    	x = X;
    }
    
    template <class Type>
    void position<Type>::setY(Type Y) {
    	y = Y;
    }
    
    template <class Type>
    void position<Type>::setZ(Type Z) {
    	z = Z;
    }
    
    template <class Type>
    void position<Type>::set(Type X, Type Y) {// 2d
    
    	setX(X);
    	setY(Y);
    }
    
    template <class Type>
    void position<Type>::set(Type X, Type Y, Type Z) {	// 3d
    	setX(X);
    	setY(Y);
    	setZ(Z);
    }
    
    //
    // Get position
    //
    template <class Type>
    Type position<Type>::getX() {
    	return x;
    }
    
    template <class Type>
    Type position<Type>::getY() {
    	return y;
    }
    
    template <class Type>
    Type position<Type>::getZ() {
    	return z;
    }
    Thanks

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Template declarations and definitions should be in the same file (usually header).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of a class in place of main( )?
    By Sebastiani in forum C++ Programming
    Replies: 15
    Last Post: 12-10-2008, 01:06 PM
  2. Need Partners (D&D fan preferably)
    By C_ntua in forum Game Programming
    Replies: 44
    Last Post: 11-22-2008, 09:21 AM
  3. typedef and typename
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2008, 08:11 PM
  4. explanation of typename keyword?
    By 7stud in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2006, 05:05 PM
  5. typedef VS typename
    By tilex in forum C++ Programming
    Replies: 9
    Last Post: 07-25-2004, 03:26 PM