what am i doing wrong with linking?

the following code gives me the errors
error: no matching function for call to ‘amax(int [4], int)’
error: no matching function for call to ‘amax(double [3], int)’
error: no matching function for call to ‘amax(long int [5], int)

i've tried all sorts of different ways and it just doesn't work for. I'm using terminal on mac osx to compile using xcode to write if it makes a difference.

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
34
35
36
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
//Definition of the function template amax() starts below:
template <typename T> 
T amax( const T[] array, const int maxSizeOfArray )
{
T maxValue = array[0];
for(int i = 0; i < maxSizeOfArray; i++)
if (array[i] > maxValue)
maxValue = array[i]; 

return maxValue;
}
//main function begins here.
int main()
{
int a[4] = {92, 764, 906, 658};
double b[3] = {322.72, 874.40, 766.86}; 
long c[5] = {-2, 4, 6, 8, 10}; 

cout << amax<int>(a, 5);
cout<<endl; 

cout<< << amax<double>(b, 4); 
cout<<endl; 

cout<< amax<long>(c, 5);
cout<<endl; 

return 0; 

}
8
9
template <typename T> 
T amax( const T array[], const int maxSizeOfArray )

Watch also your syntax on line 28.

[edit] Oh, also, you've got a fencepost error on line 25. JSYK.
Last edited on
thanks. now i'm just trying to do a work around for it to be able to do the same thing for a complex number. This thing is such a pain. i'm sure that i'm doing something simple wrong but it's soooo annoying!

i keep getting the error:
52: error: conversion from ‘std::complex<double>*’ to non-scalar type ‘std::complex<double>’ requested

edit, don't mind all the commented out stuff. i'm trying different things out.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
#include <complex>
using namespace std;
//using std::cout;
//using std::cin;
//using std::endl;
//Definition of the function template amax() starts below:
template <typename T> 
T amax( const T array[], const int maxSizeOfArray )
{
    T maxValue = array[0];
    for(int i = 0; i < maxSizeOfArray; i++)
        if (array[i] > maxValue)
            maxValue = array[i]; 
    
    return maxValue;
}

double complexarray(complex<double> array[],int size)
{
    complex<double>carray[2] = array;
    double magarray[2];
    for (int i=0; i<size; i++)
    {
        magarray[i] = abs(carray[i]);
    }
    double test = 8.2;
    return test;
}
//main function begins here.
int main()
{
    int a[4] = {92, 764, 906, 658};
    double b[3] = {322.72, 874.40, 766.86}; 
    float c[5] = {3, 4, 6, 8, 10}; 
    
    complex<double>d[2];
    d[0] = complex<double>(2.0, 4.0);
    d[1] = complex<double>(3.0, 3.0);
    
    cout<< amax<int>(a, 4);
    cout<<endl; 
    
    cout<< amax<double>(b, 3); 
    cout<<endl; 
    
    cout<< amax<float>(c, 5);
    cout<<endl; 
    
    cout<<"test" << endl;
    cout<< complexarray(d, 2);
    
    //cout<< amax<double>(d, 2) << endl;
    return 0; 
    
}
Last edited on
You cannot copy arrays like that complex<double>carray[2] = array;

You can solve it by doing like this
complex<double>carray[2] = {array[0], array[1] };
or like this
1
2
3
complex<double>carray[2];
carray[0] = array[0];
carray[1] = array[1];

but make sure size >= 2
i'm still getting the conversion error. and when i try what you mentioned it gives me

63: error: no match for ‘operator[]’ in ‘array[0]’
64: error: no match for ‘operator[]’ in ‘array[1]'
It is impossible to help you if we can't see the code.
sorry about that. i was at work and it got busy. but i got that part to work, but now to assemble the pieces... is it possible to use amax with complex<double> arrays?

i get this error again:

108: error: no matching function for call to ‘amax(std::complex<double> [3], int)’

it works with my complexarray class but i need to have just one template and use that only.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <complex>
using namespace std;
//using std::cout;
//using std::cin;
//using std::endl;
//Definition of the function template amax() starts below:
template <typename T> 
T amax( const T array[], const int maxSizeOfArray )
{
    T maxValue = 10;//array[0];
    /*for(int i = 0; i < maxSizeOfArray; i++)
        if (array[i] > maxValue)
            maxValue = array[i]; 
    */
    return maxValue;
}

complex<double> complexarray(complex<double> array[],int size)
{
    complex<double>carray[3];
    carray[0] = array[0];
    carray[1] = array[1];
    carray[2] = array[2];
    double magarray[3];
    
    for (int i=0; i<size; i++)
    {
        magarray[i] = abs(carray[i]);
    }
    double maxvalue = magarray[0];
    int x = 0;
    for (int i = 0; i<size; i++)
    {
        if (maxvalue<magarray[i])
            {
                maxvalue = magarray[i];
                x= i;
            }
    }
    
    //double test = 8.2;
    return carray[x];
}
//main function begins here.
int main()
{
    int a[4] = {92, 764, 906, 658};
    double b[3] = {322.72, 874.40, 766.86}; 
    float c[5] = {3, 4, 6, 8, 10}; 
    
    complex<double>d[3];
    d[0] = complex<double>(2.0, 4.0);
    d[1] = complex<double>(3.0, 3.0);
    d[2] = complex<double>(9.0, 12.0);
    
    cout<< amax<int>(a, 4);
    cout<<endl; 
    
    cout<< amax<double>(b, 3); 
    cout<<endl; 
    
    cout<< amax<float>(c, 5);
    cout<<endl; 
    
    cout<<"test" << endl;
    cout<< amax<double>(d, 3) << endl;
    //cout<< complexarray(d, 3) << endl;
    
    return 0; 
    
}

change amax<double>(d, 3) to amax(d, 3)
The failure is because you are trying to compare complex numbers (using >). How do you do that? The STL does not define comparison operators on complex numbers.
http://www.cut-the-knot.org/do_you_know/complex_compare.shtml

If you intend for the > operator to have meaning for complex numbers, you must declare an operator for it yourself.

Alas.
thanks peter. i forget to change the smallest things that have the biggest effect.

duoas, i'm not actually trying to compare the complex numbers, i could do it that way, but i'm going to be doing the abs() of the numbers so that way it's a double i'm comparing instead of a complex number. in theory, this would work, then i won't have to declare it myself.
Topic archived. No new replies allowed.