How can I make a function which's return type is given by the user?
How can I make a function which's return type is given by the user?
Example:Code:template <typename T> T myFunc() { T t; return t; }
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; }
>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.
same thing happens with:
Code:template< typename T > T to_dec( const std::vector< bool > & binary_number ) { T a; a++; return a; }
>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.
¬¬
you feel stressed today?
thanks anyways
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.
the type is int
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: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; 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.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; }
Ok, ill post the files:
main.cpp:
binary_funcs.h: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; }
bianry_funcs.hpp: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
The bigint file doesnt mention the binary functions so there is no need to overcharge the bandwidth with itCode: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; } }
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:The problem could be with your bigint, or it could be specific to your compiler.Code:2 - 11 0
>>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.
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