How can I make a function which's return type is given by the user?
Printable View
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. :rolleyes: 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?
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.
¬¬
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 it :)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;
}
}
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?
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 :)
Then see my above post (I just edited it a second ago). Apparently, VC.NET is 'smart' enough to let you know when you've used a variable without initializing it, and gives you a crash so that you don't end up with a bug going unnoticed.Quote:
I think the problem is that Im running the debug configuration because with the release configuration works fine also
what post?Quote:
Originally Posted by Hunter2
>>what post?
Quote:
Originally Posted by Hunter2
oh sorry =P