[Dev-C++] Templates - course examples don't work!
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2002-04-02 10:42:59
|
<html><div style='background-color:'><DIV>I'm using VC++ and the examples from my course will not compile and I know not why! It tells me A and B are not declared, but I think they are! Help me out will you?</DIV>
<DIV> </DIV>
<DIV><I><FONT color=#ff0000>
<P>// This is a program to interchange two values by using function templates.</P>
<P>#include <iostream.h></P>
<P>template<typename X></P>
<P>int Temp1(X &, X &);</P>
<P>int main()</P>
<P>{</P>
<P>int Var;</P>
<P></FONT><FONT color=#008080>int</FONT><FONT color=#ff0000> Var1 = 20;</P>
<P></FONT><FONT color=#008080>int</FONT><FONT color=#ff0000> Var2 = 25;</P>
<P>cout << "The value in Var1 = " << Var1 << endl;</P>
<P>cout << "The value in Var2 = " << Var2 << endl;</P>
<P>Var = Temp1(Var1, Var2);</P>
<P>cout << "The sum is: << Var << endl;</P>
<P>return 0;</P>
<P>}</P>
<P>template<typename X></P>
<P>int Temp1(</FONT><FONT color=#0000ff>X</FONT><FONT color=#ff0000> &A, </FONT><FONT color=#0000ff>X</FONT><FONT color=#ff0000> &B)</P>
<P>{</P>
<P></FONT><FONT color=#008000>X</FONT><FONT color=#ff0000> </FONT><FONT color=#800000>Var</FONT><FONT color=#ff0000>;</P>
<P>Var = A + B;</P>
<P>return Var;</P>
<P>}</P></I></FONT></DIV></div><br clear=all><hr>Send and receive Hotmail on your mobile device: <a href='http://g.msn.com/1HM305301/13'>Click Here</a><br></html>
|