Re: Type intializing with template

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Antti Keskinen (antti.keskinen_at_REMOVEME.ee.tpu.fi)
Date: 02/09/05


Date: Wed, 9 Feb 2005 14:26:36 +0200

Hi !

There's a syntax error lurking in your code. The definition of A::A() must
be enclosed into a template declaration, as follows:

template<class T1, class T2>
A<T1,T2>::A()
{
    t1 = gcnew T1();
    t2 = gcnew T2();
}

Template definitions and declarations cause mysterious syntax errors.
Learning the template syntax exactly is a key priority if you wish to use
it. As you can see, the simple error of lacking the template declaration
caused a head-scratching error, that was only solvable through direct
knowledge on how template class member functions must be defined.

Another (possible) error you have is in the declaration of 'class B'. You're
declaring a static variable, and as far as I remember, the assignment
operation must be done outside the class' scope, in file scope, as follows:

ref class B
{
private: // Default access priviledge. You omitted this, but it's good
policy to always write it.
        static A<X,Y>^ a;
};

A<X,Y>^ B::a = gcnew A<X,Y>::A();

Now it should compile and work properly.

-Antti Keskinen

"paul" <psegaro@yahoo.com> wrote in message
news:1107931124.890479.52850@c13g2000cwb.googlegroups.com...
> When I compile class B shown below, in VC Express 2005, I get error
> C2061: syntax error : identifier '{ctor}. Would appreciate any info on
> how to avoid the error.
>
> ref class X{
> X();
> ~X();
> };
>
> ref class Y{
> Y();
> ~Y();
>
> };
>
> template <class T1,class T2>
> ref class A {
> public:
> A();
> ~A();
> private:
> T1^ t1;
> T2^ t2;
>
> };
>
> A::A(){
> t1 = gcnew T1();
> t2 = gcnew T2();
>
> }
>
> ref class B {
> static A<X,Y>^ a = gcnew A<X,Y>::A();
>
> };
>
>
> ptrue
>



Relevant Pages

  • Re: Errors in VC program with gdiplus
    ... template argument list ... : see declaration of 'iterator' ...
    (microsoft.public.vc.mfc)
  • Errors in VC program with gdiplus
    ... template argument list ... : see declaration of 'iterator' ...
    (microsoft.public.vc.mfc)
  • Detecting and Isolating Memory Leaks Using VC6
    ... C2059: syntax error: 'constant' ... second C linkage of overloaded function '_calloc_dbg' not allowed ... : see declaration of '_calloc_dbg' ...
    (microsoft.public.vc.debugger)
  • Problem installing ruby
    ... ossl_engine.h:16: syntax error before `eEngineError' ... ossl_asn1.c:273: redeclaration of `t' ... ossl_asn1.c:490: previous declaration of `tag' ... ossl_asn1.c:712: `offset' undeclared ...
    (freebsd-questions)
  • Friend function of a template class
    ... template class. ... MyFriendFunction (MyClass * const ptrMyClass) ... after class declaration. ...
    (comp.lang.cpp)