Home > Back-end >  No matching with specified type of overloaded function "maxn" instances
No matching with specified type of overloaded function "maxn" instances

Time:01-16

C + + primer plus chapter 8, 8.8.6, programming practice topic
6
 template & lt; Typename T> 
Auto maxn (const T arr []) - & gt; T
{
Int len=sizeof (arr)/sizeof (arr [0]).
T m=arr [0];
For (int I=1; I & lt; Len. I++)
{
If (m{
M=arr [I];
}
}
return m;
}

The template & lt;> Char * maxn & lt; Char * & gt; (const char * arr [])
{
Return arr [0].
}


Error: no matching the specified type of overloaded function "maxn" instances

CodePudding user response:

The template you write wrong, you kind of int len is not the length of the array


CodePudding user response:

 template & lt; Typename T> 
Auto maxn (T & amp; Arr) - & gt; The decltype (arr [0])
{
Int len=sizeof (arr)/sizeof (arr [0]).
The decltype (arr [0]) m=arr [0].
For (int I=1; I & lt; Len. I++)
{
If (m{
M=arr [I];
}
}
return m;
}


Did not have an error, the runtime problems
Undefined reference to ` decltype ({1} parm# [0]) maxn (int *) '
collect2.exe: error: ld returned 1 exit status

CodePudding user response:

Array as a parameter, the sizeof (arr) will be the size of a pointer

CodePudding user response:

For this kind of the container traversal algorithms usually does not directly pass a container in the past, consider the realization of the STL, usually wear vessel head and tail, and to calculate
Such as sort (begin and end, CMP)

CodePudding user response:

reference CHXCHXKKK reply: 3/f
array as a parameter, sizeof (arr) will get the size of a pointer

I would like to be is in the array of reference as a parameter, and thus the length of the array can be obtained
 
The template & lt; The class T>
Int getArrayLen (T & amp; Array)
{
//coutCoutReturn (sizeof (array)/sizeof (array [0]));//call after successful
}

Then, I tried it this
 template & lt; Typename T> 
Void maxn (T & amp; Arr)
{
Int len=sizeof (arr)/sizeof (arr [0]).
The decltype (arr [0]) m=arr [0].
For (int I=1; I & lt; Len. I++)
{
If (m{
M=arr [I];
}
}
Cout}


But
 template & lt; Typename T> 
Auto maxn (T & amp; Arr) - & gt; The decltype (arr [0])
{
Int len=sizeof (arr)/sizeof (arr [0]).
The decltype (arr [0]) m=arr [0].
For (int I=1; I & lt; Len. I++)
{
If (m{
M=arr [I];
}
}
return m;//call fails, the incoming is such an array: int array=hc-positie {1}
}

CodePudding user response:

references in 4th floor, the truth is more important than right or wrong response:
for this kind of the container traversal algorithms usually does not directly pass a container in the past, consider the realization of the STL, usually wear vessel head and tail, and then calculated
Such as sort (begin and end, CMP)

Can not see the STL, I learn as soon as possible
  • Related