B
bleyddyn.apRhys
I have a bit of code that doesn't compile under MSVC++ but does under
GCC. Even under GCC, though, it seems odd that I don't need a std
namespace qualifier for the exp function.
Is this just one of those difference I have to #ifdef around, or is
there a bit of code that will compile under both?
Basically, I have a std::vector<double>. I want to replace each item
in the vector with the exp() of that item. The code does work, and
there's a simple work-around under Window's, but I'd prefer to
understand why it doesn't compile.
Below is the code and the VC++ error message. The workaround is to
replace exp with locExp in the call to transform.
Thanks for any suggestions!
Andrew
Version info:
Visual Studio 2005, Academic Edition, Version 8.0.50727.42
Visual C++ 2005 77633-233-0020534-41001
g++: powerpc-apple-darwin8-g++-4.0.1
Compiling...
transform.cpp
c:\documents and settings\asalamon\my documents\test
\transform.cpp(45) : error C2780: '_OutIt
std::transform(_InIt1,_InIt1,_InIt2,_OutIt,_Fn2)' : expects 5
arguments - 4 provided
c:\program files\microsoft visual studio 8\vc\include
\algorithm(797) : see declaration of 'std::transform'
c:\documents and settings\asalamon\my documents\test
\transform.cpp(45) : error C2914: 'std::transform' : cannot deduce
template argument as function argument is ambiguous
c:\documents and settings\asalamon\my documents\test
\transform.cpp(45) : error C2784: '_OutIt
std::transform(_InIt,_InIt,_OutIt,_Fn1)' : could not deduce template
argument for '_OutIt' from 'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=double,
_Alloc=std::allocator<double>
]
c:\program files\microsoft visual studio 8\vc\include
\algorithm(682) : see declaration of 'std::transform'
Build log was saved at "file://c:\Documents and Settings\asalamon\My
Documents\Test\testTransform\testTransform\Debug\BuildLog.htm"
testTransform - 3 error(s), 0 warning(s)
// Begin code
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath> // for exp()
void testTransformExp();
int main()
{
testTransformExp();
}
void printVec( std::vector<double> &vec )
{
for( unsigned int i = 0; i < vec.size(); ++i )
std::cout << vec << ", ";
std::cout << std::endl;
}
inline double locExp( double val )
{
return exp(val);
}
void testTransformExp()
{
std::vector<double> res( 8, 0.0 );
res[0] = 1.0;
res[1] = 2.0;
res[2] = 10.0;
res[3] = 30.0;
res[4] = 100.0;
res[5] = 400.0;
res[6] = 1000.0;
res[7] = 5000.0;
std::transform( res.begin(), res.end(), res.begin(), exp ); //
calculate the exponent of each element
printVec( res );
}
GCC. Even under GCC, though, it seems odd that I don't need a std
namespace qualifier for the exp function.
Is this just one of those difference I have to #ifdef around, or is
there a bit of code that will compile under both?
Basically, I have a std::vector<double>. I want to replace each item
in the vector with the exp() of that item. The code does work, and
there's a simple work-around under Window's, but I'd prefer to
understand why it doesn't compile.
Below is the code and the VC++ error message. The workaround is to
replace exp with locExp in the call to transform.
Thanks for any suggestions!
Andrew
Version info:
Visual Studio 2005, Academic Edition, Version 8.0.50727.42
Visual C++ 2005 77633-233-0020534-41001
g++: powerpc-apple-darwin8-g++-4.0.1
Compiling...
transform.cpp
c:\documents and settings\asalamon\my documents\test
\transform.cpp(45) : error C2780: '_OutIt
std::transform(_InIt1,_InIt1,_InIt2,_OutIt,_Fn2)' : expects 5
arguments - 4 provided
c:\program files\microsoft visual studio 8\vc\include
\algorithm(797) : see declaration of 'std::transform'
c:\documents and settings\asalamon\my documents\test
\transform.cpp(45) : error C2914: 'std::transform' : cannot deduce
template argument as function argument is ambiguous
c:\documents and settings\asalamon\my documents\test
\transform.cpp(45) : error C2784: '_OutIt
std::transform(_InIt,_InIt,_OutIt,_Fn1)' : could not deduce template
argument for '_OutIt' from 'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=double,
_Alloc=std::allocator<double>
]
c:\program files\microsoft visual studio 8\vc\include
\algorithm(682) : see declaration of 'std::transform'
Build log was saved at "file://c:\Documents and Settings\asalamon\My
Documents\Test\testTransform\testTransform\Debug\BuildLog.htm"
testTransform - 3 error(s), 0 warning(s)
// Begin code
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath> // for exp()
void testTransformExp();
int main()
{
testTransformExp();
}
void printVec( std::vector<double> &vec )
{
for( unsigned int i = 0; i < vec.size(); ++i )
std::cout << vec << ", ";
std::cout << std::endl;
}
inline double locExp( double val )
{
return exp(val);
}
void testTransformExp()
{
std::vector<double> res( 8, 0.0 );
res[0] = 1.0;
res[1] = 2.0;
res[2] = 10.0;
res[3] = 30.0;
res[4] = 100.0;
res[5] = 400.0;
res[6] = 1000.0;
res[7] = 5000.0;
std::transform( res.begin(), res.end(), res.begin(), exp ); //
calculate the exponent of each element
printVec( res );
}