Re: Type intializing with template
From: Antti Keskinen (antti.keskinen_at_REMOVEME.ee.tpu.fi)
Date: 02/09/05
- Next message: sergega4_at_yahoo.fr: "white spaces in command line"
- Previous message: Antti Keskinen: "Re: Unresolved External Symbol - problem with template class"
- In reply to: paul: "Type intializing with template"
- Next in thread: Carl Daniel [VC++ MVP]: "Re: Type intializing with template"
- Reply: Carl Daniel [VC++ MVP]: "Re: Type intializing with template"
- Messages sorted by: [ date ] [ thread ]
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
>
- Next message: sergega4_at_yahoo.fr: "white spaces in command line"
- Previous message: Antti Keskinen: "Re: Unresolved External Symbol - problem with template class"
- In reply to: paul: "Type intializing with template"
- Next in thread: Carl Daniel [VC++ MVP]: "Re: Type intializing with template"
- Reply: Carl Daniel [VC++ MVP]: "Re: Type intializing with template"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|