Templates / polymorphism - c++

I'm trying to make use of polymorphism. Basically, there's a class-name missing in the middle of the code. As I'm not used to templates, could someone give me a clue? Thank you
#ifndef TEMPLATE_H``
#define TEMPLATE_H
using namespace std;
template <class T>
class Template
{
public:
Template(int);
virtual ~Template();
virtual void push(T val);
T pop;
virtual bool isFull();
virtual bool isEmpty();
virtual void sizeOf(T val) ;
protected:
private:
int top,size;
};
#endif // TEMPLATE_H
#ifndef STACK_H
#define STACK_H
/***LIFO***/
using namespace std;
template <class S>
class stack: public Template{ // HERE, it says it's missing an expected class
-name before {
public:
stack();
virtual ~stack();
protected:
private:
};
#endif // STACK_H

stack should inherit from Template<S>, not Template.
Template is not a class. It is a class template. Template<int> would be a class, or Template<std::string>. You cannot inherit from a class template, only from a class (or struct).

Related

error: "expected unqualified-id before '{' token" While Creating a Template class

Main Class
#if defined(CONFIG_SOCKET)
template<class T>
class cUI_ScrData :public GFX_Socket
#elif defined(CONFIG_QUEUE)
template<class T>
class cUI_ScrData :public cUI_MQueueTx
#else
#endif
{
bool data_changed;
public:
T data;
UINT32 id;
void set_data(T& new_data)
{
// Some Code
}
};
Socket Class
class GFX_Socket
{
GFX_SocketImpl *gfxSocketImplObj;
public:
GFXSOCKETVSPROJ_API void Client_start();
GFXSOCKETVSPROJ_API void Client_read();
GFX_Socket();
~GFX_Socket();
void ServerWrite(void *msgbuff, int size);
void Client_start();
void Client_read();
#endif
static bool bClientStarted;
};
Queue Class
class cUI_MQueueTx
{
private:
mqd_t queue;
char ucaQueueName[QUEUE_NAME_STR_MAX_LEN];
public:
cUI_MQueueTx();
~cUI_MQueueTx();
void UI_Q_create();
};
Getting this error while creating a template class derived from two different classes. compiler is pointing out at "{" after #endif in Main Class.
Your code won't be valid in case the preprocessor goes to #else.

Why there are redefinition errors when inheritance from a base template class and put both class file in separate header files?

My base template class is in Base.h:
#include <iostream>
using std::cout;
using std::endl;
#ifndef BASE_H
#define BASE_H
template<typename T>
class Base
{
public:
Base();
Base(T a0, T b0);
void display();
private:
T a,b;
T sum();
};
#endif // BASE_H
template<typename T>
Base<T>::Base():a(0),b(0){}
template<typename T>
Base<T>::Base(T a0, T b0):a(a0),b(b0){}
template<typename T>
T Base<T>::sum()
{
return a+b;
}
template<typename T>
void Base<T>::display()
{
cout<<"The sum is: "<<sum()<<endl;
}
And my Derived.h file is:
#ifndef DERIVED_H
#define DERIVED_H
#include <Base.h>
template<typename T>
class Derived : public Base<T>
{
public:
Derived(){}
Derived(T a0, T b0);
void display1();
};
#endif // DERIVED_H
template<typename T>
Derived<T>::Derived(T a0, T b0):Base<T>(a0,b0) {}
template<T>
void Derived<T>::display1()
{
this->display();
}
I do know implementation of template class should not be in .cpp file, but why there is undefined error when I put the separate header files in different .h file?
The error is showed as follows (with code::blocks):
***include\Base.h|24|error: redefinition of 'Base<T>::Base()'|
include\Base.h|24|error: 'Base<T>::Base()' previously declared here|***
Two issues. First, your #include guards are wrong. You are only guarding the class declaration:
#ifndef BASE_H
#define BASE_H
template<typename T>
class Base
{
...
};
#endif // BASE_H
... definitions of Base<T> ...
If Base.h is #included twice, you'll only get one class declaration (good) but then you'll get multiple definitions of all the member functions (bad).
The #include guards should guard the entire file. Move the #ifndef to the first line and the #endif to the last line.
Second issue, if you provide your member function definitions in the header but external to the class declaration, you have to mark the function as inline (this can be done either in the declaration or the definition, but personally I prefer the declaration). That is:
template <typename T>
class Base {
...
inline Base();
inline void display();
inline T sum();
};
// definitions...

How to inherit Template Functions?

I am required to inherit a templated class for a project. This is what it looks like:
LinkedListInterface.h
#pragma once
#include <string>
using namespace std;
template<class T>
class LinkedListInterface
{
public:
LinkedListInterface(void) {}
virtual ~LinkedListInterface(void) {}
virtual void insertHead(T value) = 0;
virtual void insertTail(T value) = 0;
};
That's what they gave us, and now I need to make a class that inherits from that and then does various functions. I can't for the life of me figure out the proper format. This is what I have so far:
LinkedList.h
#include "LinkedListInterface.h"
#include "Node.h"
using namespace std;
class LinkedList : public LinkedListInterface<class T>
{
private:
int listsize;
Node* head;
Node* tail;
public:
LinkedList(void) {}
virtual ~LinkedList(void) {}
void insertHead(T value) {}
void insertTail(T value) {}
};
My issue is that it wont let me use T, it says Incomplete type is not allowed. Any insight on how to inherit this so I can use T would be great!
Your class needs to either be a template itself or derive from a specialized version of LinkedListInterface:
template<typename T>
class LinkedList : public LinkedListInterface<T>
{
...
};
Or:
class LinkedList : public LinkedListInterface<int>
{
...
void insertHead(int value){ }
void insertTail(int value){ }
};
LinkedList needs to be a class template with template parameter T:
template <typename T>
class LinkedList : public LinkedListInterface<T>
{
....
};

abstract classes with generics in c++ / Qt

When I run the following code and press the enter button, I get a Segmentation Fault.
I've searched everywhere on the internet, but I can't find the problem. I'm quite new to C++/Qt.
The base class:
stack.h
#ifndef STACK_H
#define STACK_H
template <class T> class stack
{
public:
stack();
virtual T pop() = 0;
virtual void push(T i) = 0;
};
#endif // STACK_H
stack.cpp
#include "stack.h"
template<class T> stack<T>::stack()
{
}
arraystack.h
#ifndef ARRAYSTACK_H
#define ARRAYSTACK_H
#include "stack.h"
template <class T> class arraystack : public stack<T>
{
public:
arraystack();
T pop();
void push(T i);
};
#endif // ARRAYSTACK_H
arraystack.cpp
#include "arraystack.h"
#include <QDebug>
template<class T> arraystack<T>::arraystack()
{
}
template<class T> T arraystack<T>::pop(){
qDebug() << "popping bad";
}
template<class T> void arraystack<T>::push(T i){
qDebug() << "pushing shit";
}
The part that calls the pop-Function:
calculator.h
// ...
private:
Ui::calculator *ui;
arraystack<int> *h;
bool integer;
// ...
calculator.cpp
// ...
void calculator::on_b_enter_clicked()
{
h->pop();
}
// ...
Error:
The inferior stopped because it received a signal from the Operating System
Signal name: SIGSEGV
Signal meaning: Segmentation Fault
This code:
A.h
template <typename T>
class A
{
public:
A(){}
virtual void f1() = 0;
};
template <typename T>
class B: public A<T>
{
public:
B(){}
void f1(){}
};
main.cpp
#include "A.h"
int main ()
{
A<int> *a = new B<int>();
a->f1();
}
Compiles and works, because all template functions are defined in header file. If you want to split the declaration and the definition, you can use one of these methods:
Include the cpp file at the bottom of your header file
Include the cpp file in main.cpp

C++ circular dependency issue with function templates

I have several classes in a project I'm working on; the first is a Solver class, originally with a function template whose full definition is in the Solver header file, like so (just showing the bare necessities):
solver.h
class Solver {
public:
template<typename T>
void solve(T t);
}
template<typename T>
void Solver::solve(T t) {
// implementation here
}
Now, class A is used as template parameter for the solve function template as follows:
A.h
#include "solver.h"
class A {
private:
Solver s; //s is instantiated in constructor
public:
void doSomething();
}
A.cpp
void A::doSomething() {
s.solve<A&>(*this);
}
So this is all fine and dandy as it is now, but for the purposes of the project, I need to move the definition of the solve() function template into an implementation file (solver.cpp) from the header file. As I understand it, I can do this as long as I add lines that explicitly state what types will be used with the function template, as follows:
solver.cpp
template<typename T>
void Solver::solve(T t) {
// implementation here
}
template void Solver::solve<A&>(A& a);
However this doesn't work when I try to compile solver, because in order to specify A as a type I want to use as a template parameter in solve.cpp, I need to have A not be an incomplete type. But A requires Solver in order to even compile - so I believe I have a circular dependency. Is there any way I can get around this issue?
I'm relatively new to all this, so take it easy on me please :) Much thanks.
Samoth is nearly right, you need class A; ("forward declaration"). But only before you use it, not before the Solver class:
Edited In response to comments, your minimal code sample was too minimal :) The real problem was Header Guards:
#ifndef SOLVER_H_INCLUDED_
#define SOLVER_H_INCLUDED_
class Solver {
public:
template<typename T>
void solve(T t);
};
#endif // SOLVER_H_INCLUDED_
And
// A.h
#ifndef A_H_INCLUDED_
#define A_H_INCLUDED_
#include "Solver.h"
class A {
private:
Solver s; //s is instantiated in constructor
public:
void doSomething();
};
#endif // A_H_INCLUDED_
// Solver.cpp
#include "Solver.h"
#include "A.h"
template<typename T>
void Solver::solve(T t) {
// implementation here
}
// explicit instantiations
template void Solver::solve<int>(int);
// ...
template void Solver::solve<A&>(A&);
This will work
// main.cpp
#include "A.h"
int main()
{
A a;
a.doSomething();
}
The best way to pass-by circular dependencies is to do this :
class A; // before the class Solver
class Solver {
public:
template<typename T>
void solve(T t);
}
template<typename T>
void Solver::solve(T t) {
// implementation here
}
What you can do is:
solver.h
#ifndef SOLVER_H_INCLUDED_
#define SOLVER_H_INCLUDED_
class Solver {
public:
template<typename T>
void solve(T t);
};
#include "solver.cpp"
#endif
solver.cpp
#include "solver.h"
template<typename T>
void Solver::solve(T t) {
// implementation here
}
and a.hpp
#ifndef A_H_INCLUDED_
#define A_H_INCLUDED_
#include "solver.h"
class A {
private:
Solver s; //s is instantiated in constructor
public:
void doSomething()
{
s.solve(*this);
}
};
#endif