Templates and type casts

This is a discussion on Templates and type casts within the C++ Programming forums, part of the General Programming Boards category; Hi, i have a simple compile time error which i cant get rid of, and was wondering if anyone can ...

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Templates and type casts

    Hi, i have a simple compile time error which i cant get rid of, and was wondering if anyone can help me out.

    Compiling...
    Lattice.cpp
    Main.cpp
    pcp2\lattice.cpp(15) : error C2440: '=' : cannot convert from 'class LatticeNode<T> *' to 'class LatticeNode<class Juxtacrine> *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    c:\program files\microsoft visual studio\vc98\include\xmemory(59) : while compiling class-template member function '__thiscall Lattice<class Juxtacrine,500>::~Lattice<class Juxtacrine,500>(void)'
    Error executing cl.exe.

    PCP2.exe - 1 error(s), 0 warning(s)
    Ok thats the error and heres the code its related to.

    Code:
    template <class T> class LatticeNode {
    
    	public:
    		int num;
    		T t;
    
    		//next/prev nodes
    		LatticeNode *next;
    		LatticeNode *prev;
    	
    		//default constructor
    		LatticeNode();
    
    	private:
    
    
    };
    
    template< class T, int size> class Lattice 
    {
    	public:
    
    		Lattice();
    		~Lattice();
    
    		int dimensions();
    		void remainder_cells();
    
    		void set_links();
    
    		void add(int i, int j);
    		void remove(int i, int j);
    
    	private:
    
    		int width, height;
    
    		LatticeNode* current;
    		LatticeNode* head;
    		LatticeNode* tail;
    
    		void init();
    };
    
    template <class T, int size> Lattice<T,size>::~Lattice()
    {
    	LatticeNode *save;
    
    	for (current = head; current != tail; current = save) {
    	    save = current->next;
    		delete current;
    	}
    	
    	delete tail;
    }
    The code in red is where i guess its referring to.

    Im not sure how to re-cast the 'save' variable into what the compiler suggests.. any ideas on how to solve this?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    In your Lattice class you need to specify template parameters to your LatticeNode pointers. Like so:

    Code:
    LatticeNode<T>* current;
    LatticeNode<T>* head;
    LatticeNode<T>* tail;
    And then in your destructor..

    Code:
    LatticeNode<T> *save;
    Also you might want to make sure you have a constructor body.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Excellent thanks, i completely missed that !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue with templates
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2008, 07:06 AM
  2. Is There a way to check a Template's Type
    By avalanche333 in forum C++ Programming
    Replies: 6
    Last Post: 01-23-2007, 05:20 PM
  3. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  4. help, templates and hashing, and yucky stuff like that
    By DarkDays in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2001, 06:01 AM

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