Sign in to follow this  
The C modest god

Templates and compilation errors in CPP

Recommended Posts

I have a problem, I will present it in the following code:
class A {
};

class B {
};

template <class T>
class Tempy {
public:
   template <class M>
   void foo(Tempy<M> src);

   T * ptr;
};

template <T>
template <M>
void
Tempy<T>::foo(Tempy<M> src);
{
  this->ptr = src.ptr; // <= Compilation error
}

void main()
{
  Tempy<A> a;
  Tempy<B> b;

  a.foo(b); // <= Cant find this
}


The problem is that I get a compilation error in a place that helps me very little. Is there a way to trace where the compilation error originates? Because it has to check in some place where the foo was called, but where do I find this foo that is the source of this compilation error? I am using visual CPP 2003. Thanks in advance.

Share this post


Link to post
Share on other sites
change
template <T>
template <M>

void
Tempy<T>::foo(Tempy<M> src);
{
this->ptr = src.ptr; // <= Compilation error
}

to
template <class T, class M>
void
Tempy<T>::foo(Tempy<M> src);
{
this->ptr = src.ptr; // <= Compilation error
}

And it should compile.


Actually, this is wrong. The template syntax, despite being weird as shit, is correct. The problem is the extra semicolon during the function definition, as well as the omission of the class keyword -

template <class T>
template <class M>
Tempy<T>::foo(Tempy<M> src);


[Edited by - Mushu on September 8, 2006 2:32:26 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
change
template <T>
template <M>

void
Tempy<T>::foo(Tempy<M> src);
{
this->ptr = src.ptr; // <= Compilation error
}

to
template <class T, class M>
void
Tempy<T>::foo(Tempy<M> src);
{
this->ptr = src.ptr; // <= Compilation error
}

And it should compile.

You didnt udnerstand what I did.
I want to get a compilation error, I just need more information.
However, I understood what is my problem.
I was looking in the task list, and there is less information in the task list then in output.
However, the output reports the compilation errors in a very messy way.
So is there a link from the tasks list into the compilation errors in the output?



Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
You didnt udnerstand what I did.
I want to get a compilation error, I just need more information.
However, I understood what is my problem.
I was looking in the task list, and there is less information in the task list then in output.
However, the output reports the compilation errors in a very messy way.
So is there a link from the tasks list into the compilation errors in the output?


Quote:
I am using visual CPP 2003.


Compiling your code gives the following output:

Compiling...
testbed.cpp
testbed.cpp(16) : error C2061: syntax error : identifier 'T'
testbed.cpp(17) : error C2061: syntax error : identifier 'M'
testbed.cpp(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation



Examination of that line (by concatenating until we hit a ; or a { gives us -

template <T> template <M> void Tempy<T>::foo(Tempy<M> src);

This is a function declaration, which is erronous because it is syntatically incorrect, because neither the class nor typename keywords were used. I don't see how those error messages are confusing, in fact, I think they say exactly what's wrong: you're missing something before T and M.

Granted, if you understand your problem you should already know all this. Maybe you should get some more experience with the compiler before you tackle things like templates - if you think these errors are nasty, wait until you see a real template error.

kthxbai.

Share this post


Link to post
Share on other sites
Once you fix that, it still won't compile because you have a stray semicolon, which means instead of an implementation, you have a re-declaration plus an (illegal) anonymous block at top level.

Once you fix that, it will still only compile for cases where M* is convertible to T* (although I assume that's what you want). In the case of A and B, that won't work; you need an inheritance hierarchy.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this