Templates and compilation errors in CPP

Started by
5 comments, last by Zahlman 17 years, 7 months ago
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.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
change
template <T>template <M>voidTempy<T>::foo(Tempy<M> src);{  this->ptr = src.ptr; // <= Compilation error}

to
template <class T, class M>voidTempy<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]
Quote:Original post by Mushu
change
template <T>template <M>voidTempy<T>::foo(Tempy<M> src);{  this->ptr = src.ptr; // <= Compilation error}

to
template <class T, class M>voidTempy<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?



It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Depending on the specific error, there is often an "instantiated here" bit that tells you where the function was called.

CM
Template error messages are notoriously unhelpful. C++0x is tossing a few ideas around that should help with it, though.
daerid@gmail.com
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.cpptestbed.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.
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.

This topic is closed to new replies.

Advertisement