# Templates and compilation errors in CPP

This topic is 4331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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]

##### Share on other sites
Quote:
 Original post by Mushuchangetemplate template voidTempy::foo(Tempy src);{ this->ptr = src.ptr; // <= Compilation error}totemplate voidTempy::foo(Tempy 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 on other sites
Depending on the specific error, there is often an "instantiated here" bit that tells you where the function was called.

CM

##### Share on other sites
Template error messages are notoriously unhelpful. C++0x is tossing a few ideas around that should help with it, though.

##### Share on other sites
Quote:
 Original post by The C modest godYou 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.

##### 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.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002003
×