Results 1 to 6 of 6

Thread: Help with template function

  1. #1

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Lightbulb Help with template function

    have a problem, I want to create a function that can handle any type and react to it accordingly. So I have used a template on my function, the problem is that I am trying to tell my function that I can only fill a string with a character array if the type of the varible is a string.

    Despite this it still churns out the old "error C2440: '=' : cannot convert from 'char [100]' to 'int'" message.

    My code is such:

    Code:
    	//// ********* EditField: Displays the old value of a field and allows the user to modify a fields data. 
    	template <class T>
        void EditField(T value)
    	{
    			char buffme[100];
    
    	    cout << "Old: " << value  << endl;
    		cout << "Enter new:->";
    		cin.getline(buffme,99);
    
    		if(typeid(value).name() == typeid(int).name()) {value = atoi(buffme); return;}
    
    		if(typeid(value).name() == typeid(string).name()) {value = buffme; return;}
    
    		if(typeid(value).name() == typeid(float).name()) {value = atof(buffme); return;}
    
    		
    	}
    Can anyone help?
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Help with template function

    In a template function all code is compiled for each type. So each of the branches of the if statement is compiled, even if they are never executed. The solution in this case is to handle the input using an overloaded function:
    Code:
    parse(buffer, value);
    
    // Interpret a string as a value of type T
    void parse(const string& buffer, string& out) { out = buffer; }
    void parse(const string& buffer, int&    out) { out = atoi(buffer); }
    //etc.
    You could also use "cin >> value" directly, this function is overloaded for most common types.

  3. #3

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: Help with template function

    Thank you, it is as I feared. I would have to overload 3 types and repeat my code for each of them.
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Help with template function

    Not at all, you only have to 'repeat' the bit that is different. You can call overloaded functions, and even other template functions, from a template function:
    Code:
    template <class T>
    void EditField(T& value) {
    	cout << "Old: " << value  << endl;
    	cout << "Enter new:->";
    	read(value);
    }
    template <typename T>
    void read(T& value) {
    	cin >> value; // handles integers and floats
    }
    void read(string& value) {
    	getline(cin, value);
    }

  5. #5

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: Help with template function

    Ah good thanks again, I am starting to regret using the string class in my program . I should have used a 2D character array:
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Help with template function

    What's that got to do with anything?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width