templated return functions

This is a discussion on templated return functions within the C++ Programming forums, part of the General Programming Boards category; How can I make a function which's return type is given by the user?...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    templated return functions

    How can I make a function which's return type is given by the user?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Example:
    Code:
    template <typename T>
    T myFunc()
    {
        T t;
        return t;
    }

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    but i get an error:
    Run-Time Check Failure #3 - The variable 'a' is being used without being defined.

    for this code:
    Code:
    template< typename T >
    T to_dec( const std::vector< bool > & binary_number )
    {
    	T	a;
    	return a;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The variable 'a' is being used without being defined.
    So do something with it. What did you think would happen? That the magical two statement function body of C++ would do whatever you wanted simply by declaring a local variable and returning it?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    same thing happens with:
    Code:
    template< typename T >
    T to_dec( const std::vector< bool > & binary_number )
    {
    	T	a;
    	a++;
    	return a;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >same thing happens with
    >T a;
    >a++;
    What a surprise. I'll spell it out in case I wasn't clear enough: Initialize the variable.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    you feel stressed today?

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    thanks anyways

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    That variable should not have to be initialized for the program to compile and or run. If the type T is a class with a default constructor the code is just fine. If you are getting a run time failure (I'm not familiar with "Run-Time Check Failure #3 - The variable 'a' is being used without being defined."), then maybe you should indicate how you are calling your function, like what type T are you using when you get this problem.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    the type is int

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Like I said before, I'm not familiar with your run-time error, so I couldn't tell you what it means or why you are getting it. However, the following code compiles and runs with only a warning with the VC++ 2003 Toolkit compiler:
    Code:
    #include <iostream>
    #include <vector>
    
    template< typename T >
    T to_dec( const std::vector< bool > & binary_number )
    {
        T    a;
        return a;
    }
    
    int main()
    {
        std::vector< bool > binary_number;
        int x = to_dec<int>(binary_number);
        std::cout << x << std::endl;
    }
    which of course makes sense, because if T is an int then it won't be initialized implicitly. If you make this simple change:
    Code:
    #include <iostream>
    #include <vector>
    
    template< typename T >
    T to_dec( const std::vector< bool > & binary_number )
    {
        T    a = 3;
        return a;
    }
    
    int main()
    {
        std::vector< bool > binary_number;
        int x = to_dec<int>(binary_number);
        std::cout << x << std::endl;
    }
    then it works perfectly. So, maybe something is wrong with the rest of your code. If you could try to give a small but compilable example of the problem then it would be easier for you to figure out and for us to help.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    Ok, ill post the files:
    main.cpp:
    Code:
    #include	<iostream>
    #include	"bigint.h"
    #include	"binary_funcs.h"
    
    using namespace std;
    using namespace bigint_ns;
    
    int main( )
    {
    	vector< bool >		in;
    	bigint< unsigned long int >		myint;
    
    	to_bin( 3, &in );
    
    	cout << in.size( ) << " - " << in[0] << in[1]<< endl;
    	cout << to_dec<int>(in) << endl;
    	string		my;
    	return 0;
    }
    binary_funcs.h:
    Code:
    #ifndef		_F_BINARY_FUNCS_
    #define		_F_BINARY_FUNCS_
    
    #include	<vector>
    #include	<algorithm>
    
    
    namespace bigint_ns
    {
    	//Converts a [number] to binary
    	template< typename T >
    	bool	to_bin( const T & number, std::vector< bool > * binary_number, const int output_size = -1 );
    
    	//Converts a binary number into a decimal number
    	template< typename T >
    	T to_dec( const std::vector< bool > & binary_number );
    }
    
    #include "binary_funcs.hpp"
    
    #endif
    bianry_funcs.hpp:
    Code:
    namespace bigint_ns
    {
    
    	template< typename T >
    	bool	to_bin( const T & number, std::vector< bool > * binary_number, const int output_size )
    	{
    		T	num = number;
    
    		while( num > 0 )
    		{
    			binary_number->push_back( num % 2 );
    			num /= 2;
    		}
    
    		std::reverse( binary_number->begin( ), binary_number->end( ) );
    
    		if( output_size == -1 )
    			return true;
    
    		//We have to add extra zeros
    		int			zeros = output_size - binary_number->size( );
    		for( ; zeros >= 0 ; --zeros )
    		{
    			binary_number->insert( binary_number->begin( ), false );
    		}
    
    		return true;
    	}
    
    	template< typename T >
    	T to_dec( const std::vector< bool > & binary_number )
    	{
    		T	a;
    	
    
    		
    		return a;
    	}
    
    }
    The bigint file doesnt mention the binary functions so there is no need to overcharge the bandwidth with it

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Works fine for me...

    I combined all the code into a single file based on where the #includes were. I removed the bigint.h include and the line in main referring to the bigint. I compiled it and got the unitialized variable warning (as I should), and ran it without error. I then changed the code to initialize the variable to 0 (as you should for now until you implement the function). It compiled with no errors and output:
    Code:
    2 - 11
    0
    The problem could be with your bigint, or it could be specific to your compiler.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>binary_number->end( )
    That might be the problem; end() returns an iterator that points to one past the last element of the sequence. However, I'm not familiar with the reverse() algorithm.

    >>for( ; zeros >= 0 ; --zeros )
    Not 100% sure, but you might want to use > instead of >=.

    Other than that, I can't think of anything that would give you a runtime error.

    **EDIT**
    >>Run-Time Check Failure #3 - The variable 'a' is being used without being defined.
    Sounds like a debugger feature or something to me. Have you event tried initializing the variable yet?
    Last edited by Hunter2; 10-11-2004 at 05:13 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    my compiler is Visual c++ .NET. I think the problem is that Im running the debug configuration because with the release configuration works fine also

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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