I've just begun learning C++

got some problems with error finding...

I know there are some errors in these program segments

but not sure exactly what they are

nor know how to explain

please help me out ...

: )

ps. For a particular program segment,

there may be no errors 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** );

got some problems with error finding...

I know there are some errors in these program segments

but not sure exactly what they are

nor know how to explain

please help me out ...

: )

ps. For a particular program segment,

there may be no errors present in the segment.

a)

{

}

b)

{

cout << "The sum is " << x + y << '\n';

}

c)

A product(A num1, A num2, A num3 )

{

}

d)

I've just begun learning C++ |

a) this code will compile although the template is useless - type A is not used in the function definition

b) void function can't contain return statement, change return type to int

`int` printResults(`int` x, `int` y)

c) class or typename missing

`template` <`class` A>

or `template` <`typename` A>

d) ok

I'm not sure about d)

I believe you cannot overload a function by its return value, only by its paramter list.

I believe you cannot overload a function by its return value, only by its paramter list.

I believe you cannot overload a function by its return value, only by its paramter list. |

Yes, you're right. I meant it's OK if used as independent declarations (in different program codes). Sorry for confusion.

I've learnt a little about C last term

and this term we're going on to C++

I didn't catch up with progress last term

so kind of have a big problem now

simply just using templates gives me a big headache~

anyway,

thank you all sooooooooo much ^ ^

and this term we're going on to C++

I didn't catch up with progress last term

so kind of have a big problem now

simply just using templates gives me a big headache~

anyway,

thank you all sooooooooo much ^ ^

Topic archived. No new replies allowed.