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

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.

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?

C++ template instantiation, error: member of non-class type 'int'

I am trying to implement a binary search tree in C++.
I have run into a problem while recursively calling function Node<T>::append in its own definition.
Here is a minimum reproducible example:
#include <iostream>
#include <string>
#include <memory> // std::unique_ptr<>
using namespace::std;
template<class T> class Node {
public:
// constructors
Node() {};
Node(const T&);
// operations
void append(const T&);
void print();
private:
unique_ptr<T> value, left_child, right_child;
};
template<class T> class BinaryTree {
public:
// constructors
BinaryTree() {};
BinaryTree(const T&);
// operations
void insert(const T&);
void output();
private:
Node<T> root;
int size;
};
template<class T> Node<T>::Node(const T& in): value(new T (in)), left_child(nullptr), right_child(nullptr) {}
template<class T> void Node<T>::append(const T& in) {
if (in < *value) {
if (left_child)
left_child->append(in);
else
left_child(new Node(in));
} else if (in > *value) {
if (right_child)
right_child->append(in);
else
right_child(new Node(in));
}
}
template<class T> void Node<T>::print() {
cout << string(6,' ') << "( " << *value << " ) " << endl;
if (left_child)
left_child->print();
if (right_child) {
cout << string(10,' ');
right_child->print();
}
}
template<class T> BinaryTree<T>::BinaryTree(const T& in): root(in), size(1) {}
template<class T> void BinaryTree<T>::insert(const T& in) {
root.append(in);
}
template<class T> void BinaryTree<T>::output() {
root.print();
}
int main()
{
BinaryTree<int> test(5);
test.insert(3);
test.insert(9);
test.output();
return 0;
}
g++ logs in the following error:
error: request for member 'append' in
'*((Node<int>*)this)->Node<int>::left_child.std::unique_ptr<_Tp, _Dp>::operator-><int, std::default_delete<int> >()',
which is of non-class type 'int' left_child->append(in);
I think the compiler sees the line left_child->append(in); not as a recursive call but as some functor to a function that does not exist.
How can I solve this issue?
See online compilation: https://godbolt.org/z/Pna9e5
left_child and right_child do not point Node. The compiler explains this pretty clearly: which is of non-class type 'int' left_child, left_child is of type int, not class. The declarations
unique_ptr<T> value, left_child, right_child;
should be
unique_ptr<T> value;
unique_ptr<Node<T>> left_child, right_child;
The further issue: left_child(new Node(in));, left_child is not a function, the statement must be left_child.reset(new Node(in));.

Error: Class A is not a base of Class B 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;
};
}

/Users/rstu/workspace/.../main.cpp:38:13: No matching conversion for C-style cast from 'void' to 'std::__1::basic_string<char>'

It works for int, but not for std::string?
I do not understand the following error (what am I doing wrong?):
template <class T> class Stack {
private:
vector<T> elems;
public:
void push(T const&);
T pop();
};
template <class T> void Stack<T>::push (T const& elem) {
elems.push_back(elem);
}
template <class T> T Stack<T>::pop () {
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
T ret = (T)elems.pop_back(); /* <--- ERROR! <<================ */
return ret;
}
main(){
...
string X = stringStack.pop();
stringStack.pop();
...
}
Error: No matching conversion for C-style cast from 'void' to 'std::__1::basic_string'
Check the documentation for vector<T>::pop_back(). It doesn't return anything.

const and pointers (a node example)

Could you please tell me why I get this error:
source_file.cpp(41) : error C2664: 'void std::vector<Node<int> *,std::allocator<_Ty>>::push_back(Node<int> *const &)'
: cannot convert argument 1 from 'const Node<int> *' to 'Node<int> *&&'
when I call the AddChild method?
Here's my class definition/implementation:
template<class T>
class Node
{
private:
T _value;
vector<Node*> children;
public:
Node(T value);
Node(const Node<T>& node);
void AddChild(const Node<T>* node);
T getValue() const;
vector<Node<T>*> returnChildren() const;
~Node();
};
template <class T>
Node<T>::Node(T value):_value(value)
{
}
template <class T>
Node<T>::Node(const Node& node):_value(node.getValue()),
children(node.returnChildren())
{
}
template <class T>
void Node<T>::AddChild(const Node* node)
{
children.push_back(node);
}
template <class T>
T Node<T>::getValue() const
{
return _value;
}
template <class T>
vector<Node<T>*> Node<T>::returnChildren() const
{
return children;
}
template <class T>
Node<T>::~Node()
{
for (vector<Node*>::iterator it=children.begin() ; it!=children.end() ; it++)
{
delete (*it);
}
}
children contains pointers-to-non-const.
The node parameter is a pointer-to-const.
Those types are incompatible.
Either store pointers-to-const in children, or make the node parameter a pointer-to-non-const.