🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by nguyen Tue, 28 Jul 1992 21:35
template syntax
#305
Author: nguyen
Date: Tue, 28 Jul 1992 21:35
62 lines
2122 bytes
<..> should be specified in all functions inside a class header,
except constructor and destructor. ARM p.350.

The following code compiles with AT&T but not with GNU g++2.0
(gcc-2.2.2). Who is wrong?

----------------------------
#include <iostream.h>


template<class T1, class T2>
class foo{
public:
  T1 value1;
  T2 value2;
  foo(T1 a, T2 b) { value1 = a; value2 = b;}
  void bar(foo<T1,T2>& t) { value1 = t.value1; value2 = t.value2;} //********
  void print() { cout << value1 << " " << value2 << endl;}
};



main()
{
  foo<int, int> fi(10, 0), fii(0, 5);
  foo<float, int> ff(5.0, 0);
  foo<char, int> fc('c', 1);


  fi.print();
  fii.print();
  fii.bar(fi);
  fii.print();
  ff.print();
  fc.print();
  
  cout << "hello" << endl;
}
---------------------------
% /home/thuban/software3/gnu/bin/g++ -I /home/thuban/software3/gnu/lib/g++-incl
ude gnu_template.C
gnu_template.C:10: parse error before `,'
gnu_template.C:10: parse error before `,'
gnu_template.C:10: parse error before `,'
gnu_template.C: In method `void  foo<char ,int>::bar (...)':
gnu_template.C:10: `t' undeclared (first use this function)
gnu_template.C:10: (Each undeclared identifier is reported only once
gnu_template.C:10: for each function it appears in.)
gnu_template.C: In method `void  foo<float ,int>::bar (...)':
gnu_template.C:10: `t' undeclared (first use this function)
gnu_template.C: In method `void  foo<int ,int>::bar (...)':
gnu_template.C:10: `t' undeclared (first use this function)
-------------

Van-Duc Nguyen
General Electric 
Research & Development Ctr
1 River Road, Room K1-5C39.
Schenectady, NY 12301.
Phone: (518) 387-5659
Fax:   (518) 387-6845
nguyen@crd.ge.com
Re: template syntax
#336
Author: Joe Buck
Date: Wed, 29 Jul 1992 02:17
29 lines
1233 bytes
In article <9207282135.AA11012@etamin.crd.Ge.Com> nguyen@sol.crd.ge.com writes:
>The following code compiles with AT&T but not with GNU g++2.0
>(gcc-2.2.2). Who is wrong?

gcc/g++ is wrong, but it's not surprising given the ambiguity of some of
the template specs. If you change the problem line in your template class
definition from

>template<class T1, class T2>
>class foo{
>public:
>  T1 value1;
>  T2 value2;
>  foo(T1 a, T2 b) { value1 = a; value2 = b;}
>  void bar(foo<T1,T2>& t) { value1 = t.value1; value2 = t.value2;} //********
>  void print() { cout << value1 << " " << value2 << endl;}
>};

to

	...
  void bar(foo& t) { value1 = t.value1; value2 = t.value2;}
	...

your program compiles, runs, and produces the right answers.


--
Joe Buck	jbuck@ohm.berkeley.edu
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads