(Find the Error) Determine whether the following program segments contain errors. For each error, explain how it can be corrected. [Note: For a particular program segment, it’s possible that no errors are present in the segment.]

a) ```
template < class A >
int sum( int num1, int num2, int num3 )
{
return num1 + num2 + num3;
}
```
b) ```
void printResults( int x, int y )
{
cout << "The sum is " << x + y << '\n';
return x + y;
}
```
c) ```
template < A >
A product( A num1, A num2, A num3 )
{
return num1 * num2 * num3;
}
```
d) ```
double cube( int );
int cube( int );
```

a) Error: The function return type and parameter types are int.
Correction: The function return type and parameter types should be A or the function should not be a template.
b) Error: The function specifies a void return type and attempts to return a value.
Two possible solutions: (1) change void to int, or (2) remove the line return x + y;.
c) Error: Keyword class is missing in the template declaration.
Correction: Insert keyword class (or keyword typename), as in template .
d) Error: The signatures are not different. Overloaded functions must have different signatures—the name and/or parameter list must be different. If only the returns types differ, the compiler generates an error message.
Correction: Change either the name or parameter list of one of the functions.

Computer Science & Information Technology

You might also like to view...

External modems are devices that contain a

A) 15-pin connector B) USB port C) RJ-11 port D) Network port

Computer Science & Information Technology

Answer the following questions true (T) or false (F)

1. Given the two C++ array declarations: int a[10], b[10]; You can successfully compute one array, say a, then assign b to a: a = b; 2. In the sequential search algorithm, items are examined alternately, odd then evens, in order to find whether the target value is in the array (and if the target is present, to the index of the target.)

Computer Science & Information Technology