Error: Class A is not a base of Class B C++ - c++

When I try to call the function in line **
through this function:
template<class T,int SIZE>
T const& Array<T,SIZE>::const_iterator::operator*() const {
return this->Array<T,SIZE>::iterator::operator*();
}
It gives me the following error:
error: 'Array<int, 3>::iterator' is not a base of 'const Array<int, 3>::const_iterator'
Can anyone explain why?
template <class T, int SIZE>
class Array {
T data[SIZE];
public:
explicit Array();
Array(const Array& a); //copy constructor
~Array(); //destructor
class iterator {
Array<T,SIZE>* array;
int index;
public:
iterator(Array<T,SIZE>* array,int index);
T& operator*() const; //**
};
class const_iterator {
const Array<T,SIZE>* array;
int index;
public:
T const& operator*() const;
};
}

Related

I'm facing problem with operator overloading during constructor call

Here I've implemented a class called Vector and tried to overload = operator with a parameter initialized_list.
#include<iostream>
using namespace std;
template<class T>
class Vector
{
private:
T* arr;
int _size;
int capacity;
public:
Vector();
~Vector();
Vector<T>& operator = (const initializer_list<T>& l);
};
template<class T>
Vector<T>& Vector<T>::operator = (const initializer_list<T>& l)
{
_size=capacity=l.size();
arr=new T[l.size()];
return *this;
}
template<class T>
Vector<T>::Vector()
{
_size=0;
capacity=0;
}
template<class T>
Vector<T>::~Vector()
{
delete[] arr;
}
In this implementation if I run with the code below it works fine.
int main()
{
Vector<int>v;
v={1,2,3};
}
But if I want to run with this code below it shows error: could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'Vector<int>'
int main()
{
Vector<int>v={1,2,3};
}
How can I fix this problem to make it work as the code above?

invalid use of 'this' outside of a non-static member function?

I wrote the following code inside the public part in my Matrix<T> class:
#include <iostream>
#include "Auxiliaries.h"
namespace mtm {
template<class T>
class Matrix {
private:
Dimensions dimensions;
T *data;
public:
iterator_impl<T> begin(){};
class AccessIllegalElement;
class IllegalInitialization;
class DimensionMismatch;
Matrix(const Dimensions &matrix_dimensions, const T &initial_value = T());
};
/**Iterators**/
template<typename T>
class iterator_impl;
template<typename T>
iterator_impl<T> begin(){
return iterator(this, 0);
}
template<typename T>
class iterator_impl{
private:
const Matrix<T> *matrix;
int index;
friend class Matrix<T>;
public:
iterator_impl(const iterator_impl &) = default;
iterator_impl &operator=(const iterator_impl &) = default;
~iterator_impl() = default;
iterator_impl(const Matrix<T> *matrix, int index)
: matrix(matrix), index(index) {}
iterator_impl &operator++()
{
++index;
return *this;
}
iterator_impl operator++(int)
{
iterator_impl result = *this;
++*this;
return result;
}
bool operator==(const iterator_impl &it) const
{
return index == it.index;
}
bool operator!=(const iterator_impl &it) const
{
return !(*this == it);
}
T &operator*() const {
return matrix->data[index];
}
};
template<typename T>
using iterator = iterator_impl<T>;
template<typename T>
using const_iterator = iterator_impl<const T>;
}
But I'm getting the following error:
invalid use of 'this' outside of a non-static member function
return iterator(this, 0);
What did I do wrong here and how may I solve this one?
my class:
template<class T>
class Matrix {
private:
Dimensions dimensions;
T *data;
public:
iterator_impl<T> begin(){};
//....
}
https://wandbox.org/permlink/R4rQjGVNZWUHtMqj
What should this mean in this context?
template<typename T>
iterator_impl<T> begin(){
return iterator(this, 0);
}
It should be a pointer to the object of the class the method belongs to, but the begin function is a free function that is not a member of any class.
How do you plan to use this function? Basic patterns are:
container.begin();
or
begin(container);
In both cases there is a container: the object of the class you have forgotten in your sample.
Update
According you to your updates, this should be the pointer to the Matrix object. You have already declared the method, not let's define it. The implementation is actually up to you, I'm not sure if it is correct. The key part is that you have forgotten to specify the class in the function signature:
template<typename T>
iterator_impl<T> Matrix<T>::begin() {
return iterator(this, 0);
}

Problems creating a buffer in C++

I have create a buffer structure like this:
template <typename T>
class locked_buffer {
public:
locked_buffer(int n);
locked_buffer(const locked_buffer &) = delete;
~locked_buffer() = default;
int size() const noexcept;
bool empty() const noexcept;
bool full() const noexcept;
void put(const T & x, bool last) noexcept;
std::pair<bool,T> get() noexcept;
private:
int next_position(int p) const noexcept;
bool do_empty() const noexcept;
bool do_full() const noexcept;
private:
struct item {
bool last;
T value;
};
const int size_;
std::unique_ptr<item[]> buf_;
int next_read_ = 0;
int next_write_ = 0;
mutable std::mutex mut_;
std::condition_variable not_full_;
std::condition_variable not_empty_;
};
template <typename T>
locked_buffer<T>::locked_buffer(int n) :
size_{n},
buf_{new item[size_]}
{
}
template <typename T>
int locked_buffer<T>::size() const noexcept
{
return size_;
}
But when I try to use it in my main function,
locked_buffer <std::pair<int, std::vector<std::vector<unsigned char>>>> buffer1;
I obtain an error like this:
error: missing template arguments before ‘std’ locked_buffer <std::pair<int, std::vector<std::vector<unsigned char>>>>buffer1;
I think is probably I'm not creating the template properly, but at this point I'm very frustated because I can't reach any proper solution.
Thanks.
As #TomaszPlaskota said, I created an object of type locked_buffer with n elements, where 'n' represents the size of the buffer that I'm creating.
locked_buffer <std::pair<int, std::vector<std::vector<unsigned char>>>> buffer1(n);

erreur: cannot convert ‘Cell<int>*’ to ‘List<int>*’ in assignment

I have a problem in my function that add an element in my linked list.
Here is my function code :
template <class T> class Cell
{
public:
Cell<T>* suivant;
T data;
//Constructeur & Destructeur
Cell(T Val);
};
template <class T> class List
{
private:
List<T>* tete;
List<T>* queue;
int longeur;
public:
//Constructeur & Destructeur
List<T>(void);
~List<T>(void);
int ajout_en_tete (T Val);
int ajout_en_fin (List<T>& a, T Val);
void concat (List<T>& a , const List<T>& b);
void copie (const List<T>& a, List<T>& b);
int supprimer (List<T>& a, int pos);
void supprimer_liste(void);
int Taille (const List<T>& a);
int acces (const List<T>& a , int pos);
void afficher (List<T>& a);
void test_vide(List<T>& a);
};
template <class T> List<T>::List(void)
{
tete = NULL;
queue = NULL;
longeur=0;
}
template <class T> List<T>::~List(void)
{
supprimer_liste();
}
template <class T> Cell<T>::Cell(T Val)
{
suivant = NULL;
data = Val;
}
template <class T> int List<T>::ajout_en_tete(T Val)
{
Cell<T>* C = new Cell<T>(Val);
if(longeur==0)
{
tete=C;
queue=C;
longeur+=1;
}
else
{
C->suivant=tete;
tete=C;
longeur+=1;
}
return 0;
}
I have this error that i dont understand the meaning:
src/main.cpp:16:19: instantiated from here
src/liste.h:73:24: erreur: cannot convert ‘Cell<int>*’ to ‘int*’ in initialization
src/liste.h:76:3: erreur: cannot convert ‘int*’ to ‘List<int>*’ in assignment
src/liste.h:77:3: erreur: cannot convert ‘int*’ to ‘List<int>*’ in assignment
src/liste.h:84:3: erreur: request for member ‘suivant’ in ‘* C’, which is of non-class type ‘int’
src/liste.h:85:3: erreur: cannot convert ‘int*’ to ‘List<int>*’ in assignment
I assume the first error refers to this line
tete=C;
tete is of type List<T>*, and C is of type Cell<T>*. These types aren't the same and so you get an error.
Looking at your code it's pretty clear that this
private:
List<T>* tete;
List<T>* queue;
should really be this
private:
Cell<T>* tete;
Cell<T>* queue;
because a List holds pointers to Cells not pointers to more Lists.
A List should contain Cells, not Lists. You probably need to fix the type of the member(s):
template <class T> class List
{
private:
Cell<T>* tete; // <- here
Cell<T>* queue; // <- here
although I'm not sure for queue as I don't know what it is supposed to do.

error: no match for 'operator=' . Trying to inherit from base class and initialize from base class?

I'm trying to declare a base class called BASE and I'm having trouble using the inherited types A and B inside of the BASE class. I'm getting the error
|In member function 'NODE& NODE::operator=(const NODE&)':|
16|warning: no return statement in function returning non-void|
In member function 'void BASE<T, SIZE>::init_A(int) [with T = NODE, unsigned int SIZE = 2u]':|
96|instantiated from here|
39|error: no match for 'operator=' in 'A<NODE, 2u>::DATA[index] = a'|
13|note: candidates are: NODE& NODE::operator=(const NODE&)|
#include <iostream>
class NODE
{
private:
public:
NODE(){}
~NODE(){}
};
template <class T, size_t SIZE>
class A;
template <class T, size_t SIZE>
class BASE
{
protected:
static T DATA[SIZE];
public:
BASE()
{
}
~BASE(){}
void init_A(int index)
{
A<T,SIZE>::DATA[index] = T();
}
};
template <class T, size_t SIZE>
class A : public BASE<T,SIZE>
{
protected:
public:
A(){}
~A(){}
};
template <class T, size_t SIZE>
T BASE<T,SIZE>::DATA[SIZE] = {};
int main()
{
BASE<NODE,2> base;
base.init_A(0);
return 0;
}
I can make it compile, but it might not do what you want.
The first problem is you assignment operator promises to return something and doesn't:
NODE& NODE::operator=(const NODE&)
{
}
Try this
NODE& NODE::operator=(const NODE&)
{
return *this;
}
The second problem is
A<T,SIZE> a;
A<T,SIZE>::DATA[index] = a;
The DATA is an array of T, not an A<T,SIZE>.
Try this
A<T,SIZE>::DATA[index] = T();
Finally you need to declare your statics somewhere.
Finally you need to define your statics somewhere.See here