Could not deduce template argument error

Hello,
This function works fine :

1
2
3
4
5
6
7
template<class T> mValArray<T> abs (const mValArray<T>& x)
{
    mValArray<T> x2(x);
     for(mValArray<T>::iterator it = x2.begin(); it != x2.end(); it++)
     *it = abs(*it); 
    return x2;
}


But this function is bugging me :
1
2
3
4
5
6
7
template<class T> mValArray<T> acos (const mValArray<T>& x)
{
    mValArray<T> x2(x);
     for(mValArray<T>::iterator it = x2.begin(); it != x2.end(); it++)
     *it = acos(*it); 
    return x2;
}


It says : error C2784: 'class mValArray<T> __cdecl acos(const class mValArray<T> &)' : could not deduce template argument for 'const class mValArray<L> &' from 'int'

When trying the code :
1
2
3
4
5
int val[] = {0.0, 0.25, 0.5, 0.75, 1.0};
mValArray<int> foo (val, 5);

mValArray<int> bar = abs (foo); // fine
mValArray<int> bar2 = acos (foo); // error 


What is wrong with my code? Thanks.
For std::acos etc., consider using items of type double instead of type int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

template < typename T > using mValArray = std::vector<T> ;

template<class T> mValArray<T> abs( const mValArray<T>& x )
{
    mValArray<T> x2(x);
    for( auto& v : x2 ) v = std::abs(v) ;
    return x2;
}

template<class T> mValArray<T> acos( const mValArray<T>& x )
{
    mValArray<T> x2(x);
    for( auto& v : x2 ) v = std::acos(v) ;
    return x2;
}

int main()
{
    mValArray<double> foo = { 0.0, 0.25, -0.5, 0.75, -0.99 } ;

    const auto bar = abs(foo) ;
    for( double v : bar ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    const auto baz = acos(foo) ;
    for( double v : baz ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/a311866b38ad4287
http://rextester.com/HSOM68925


Consider using std::valarray<>; it has direct support for element-wise (common) mathematical operations.
http://en.cppreference.com/w/cpp/numeric/valarray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <valarray>

template < typename T > using mValArray = std::valarray<T> ;

int main()
{
    mValArray<double> foo = { 0.0, 0.25, -0.5, 0.75, -0.99 } ;

    const auto bar = std::abs(foo) ;
    for( std::size_t i = 0; i < bar.size(); ++i ) std::cout << bar[i] << ' ' ;
    std::cout << '\n' ;

    const auto baz = std::acos(foo) ;
    for( std::size_t i = 0; i < baz.size(); ++i ) std::cout << baz[i] << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/0db77af42c47932a
http://rextester.com/QBC1199
@TheIdeasMan
Please do not derail someone else's thread. If these are not clear and sufficent enough, you can ask for more. Please don't post off-topic posts that do not contribute to solving the problem.
Please do not derail someone else's thread


Once a troll, always a troll. Don't expect to get any quarter from me. If admin isn't happy, he will let me know. I don't take instructions from you.
@JLBorges (thumb-up)
+1

The problem has yet to be solved, but let's leave it at that for now. If something fails, I might as well as try the alternative :)
Problem solved!!!

I forgot to include this header :
#include <cmath>

Thanks everybody!!!
Topic archived. No new replies allowed.