Class is not a class template - c++

i get the error : class is not a class template .Any idea why?
template<class T>
class nod{
friend class lista<T>;
protected:
T info;
nod<T> *urm,*prec;
};

lista is not known yet at this point in the code. So of course the compiler doesn't think it's a template class. You need to forward declare it with its template arguments. See also: How to forward declare a C++ template class?

Related

C++: Can a Template be friend of a Class?

Is it possible to make friend of a class, all possible variants of a class-template?
Just to clarify, for example, something like this:
class A
{ friend template B; } // syntactic error yeah
So any B<X> variant could manipulate any protected attribute of A.
A is an small and simple class with a lot of friends who manipulate its attributes. Just one of then need to be a template. I know that I can do this:
template <class T>
class A
{ friend class B<T>; }
But so I would have to change my code in all the other friends and I would like to avoid it.
You may define a friend template class like that:
class A{
template<typename T>
friend class B;
};
That would make every specialization of class B a friend of class A. I've had a similar question that had the opposite goal: to restrict some specializations: Friend template function instantiations that match parameter

Accessing a class object within a class from a class

I know this title is a bit confusing but a diagram will probably help.
in one file:
#ifndef ...
#def ...
#includes (including OListiterator.h>
template <class T>
class OList;
template <class T>
Class OListiterator{
friend class OList<T>::OList;
typename OList<T>::Node* iiter;
};
function defs;
#endif
in another:
#ifndef ...
#def ...
#includes (one is OList.h)
template<class T>
class OListiterator;
template <class T>
Class OList{
friend class OListiterator<T>::OListiterator;
public:
class Node{
};
//things
};
//functions
#endif
This is what myself and the TA assisting me would have thought would work, but I am thrown the error: Node does not name a type in OList. Does anybody know why/how I can fix this? Let me know if I need to post more.
It looks like there is a dependency here. OList needs OListIterator, and vice versa. The way to solve this is to change your design so this dependency is not needed. Try placing your OListIterator design so it can be used inside the OList.
You need a forward declaration of class OList:
// forward declaration
template <class T>
class OList;
template <class T>
class OListiterator{
friend class OList<T>::OList;
typename OList<T>::Node* iiter;
};
template <class T>
class OList{
friend class OListiterator<T>::OListiterator;
public:
class Node{
};
};
Demo
Note that friend class OList<T>::OList; is equivalent to just friend class OList<T>; because OList<T>::OList is class name injected in scope of OList<T>.
When you name the member Node of OList as a friend, the compiler needs the entire definition of OList to ensure that there is such a member.
The solution would be to break the dependency. Why does a list node need to have access to the list iterator's internals? Seems to me like it should be the other way around.

why Specialized template class need forward declaration?

such is the code:
template<typename,int> class Uoo; //without this will result in complie error,why?
template<typename T>
class Uoo<T,1>
{
};
int main(){
return 0;
}
why Specialized template class need forward declaration?
The following code is a specialisation of a template.
template<typename T>
class Uoo<T,1>
{
};
But you haven't said what the unspecialised form is, and the language requires you to do that. So you need to add the prototype:
template<typename,int> class Uoo;
You don't actually need to declare the unspecialised form since an instance of it is never required. So a prototype is sufficient.
It's not actually a forward declaration that you're making. What you are doing is first defining the "pattern" of the templated class and then you're defining a specialized context or version of it. The better question is, if you didn't have a non-specialized case, then what would be the point of the 2nd template parameter?
As declared, template<typename T> class Uoo<T,1> is a partial specialization of template<typename,int> class Uoo; it fixes the int parameter to 1. It cannot be a partial specialization of a template that doesn't exist.
You could make your "real" class template self-sufficient by writing
template<typename T>
class Uoo
{
...
};

Curiously recurring templates with template leaf classes

I am thinking about using curiously recurring template pattern for my application. However, I would like the classes to operate on the user defined types. I would like to understand if it is possible to create a structure similar to the one shown below:
template <class T_leaftype>
class BaseTrajectoryPoint {
};
template <class MyType>
class MyTrajectoryPoint: public BaseTrajectoryPoint<MyTrajectoryPoint> {
private:
MyType A;
};
The code above fails to compile with the following error:
type/value mismatch at argument 1 in template parameter list for ‘template class BaseTrajectoryPoint’
Are there any alternative ways of approaching the problem? I would like to use static polymorphism, but I would prefer to define all possible methods in the base class.
template <class T_leaftype>
class BaseTrajectoryPoint {
};
template <class MyType>
class MyTrajectoryPoint: public BaseTrajectoryPoint<MyTrajectoryPoint<MyType> > {
private:
MyType A;
};
MyTrajectoryPoint isn't a type, it's template; when you pass it as template parameter, it's seen as template<typename> class T>, not template<class T> - and the latter is what your base class is expecting. But MyTrajectoryPoint<MyType> names a type, so you can use it as template parameter of your base class.
Of course, you can change declaration of BaseTrajectoryPoint to template<template<class> class T_leaftype>, but then you would have to use class template as template parameter, never a complete type.
What our friend Griwes said is correct, although if you know that every class that will inherit BaseTrajectoryPoint is a template class, you can do the following:
template<template < class > class TLeaf> // << This means: It is expected a template class as parameter
class BaseTrajectoryPoint{
};
template <class MyType>
class MyTrajectoryPoint: public BaseTrajectoryPoint<MyTrajectoryPoint> >{
private:
MyType A;
};

Can one template class can be friend of another template class in c++?

I have a two template class, templateClass1 and templateClass2. I want to use private variables and methods of templateClass1 in templateClass2. Is it possible to do so by using friend keyword in c++ ?
Sumit
I know this post is probably dead, but for other people who stumble upon this...
templateClass1.h
template <class T> class templateClass2; // forward declare
template <typename T>
class templateClass1 {
friend templateClass2<T>;
};
templateClass2.h
template <class T> class templateClass1; // forward declare
template <typename T>
class templateClass2 {
friend templateClass1;
}
It is possible to have a friends of any type, but a template is not a type until its template arguments have been supplied. So in general you would have to have a specialization for each full type you wish to be friends with. This will push you toward attempting to pass the type to be friend as a template parameter, but you can not supply a template type that will be friended.
for ex. this is illegal
template <class T>
class A
{
friend class T;
};
with those stipulations it makes it very difficult to do anything meaning full with templates and friendedness.