Making personalize stack with personalized class but error shows up - c++

Hi I wanted to make my personal stack class with template
and make a stack with personal class.
template <class T>
class stack{
public:
stack(T);
~stack();
void insert(T);
T pop();
void printstack();
private:
int top;
int capacity;
T *list;
};
template <class T>
stack<T>::stack(T first){
top=0;
capacity=10;
list = new T[capacity];
memset(cards, 0, sizeof(T)*10);
list[0]=first;
}
and I wanted make a stack with card method
class node{
public:
node(int i=0,int j=0);
~node();
private:
int node_num;
int nodenum;
};
class list{
public:
list();
~list();
private:
stack<node> nodelist;
};
list::list(){
stack<node> nodelist(node(1,1));
}
But when I run the code error shows up
"Constructor for 'deck' must explicitly initialize the member 'decklist' which does not have a default constructor"
it works fine when I use stack deck1(1); what might be the problem?
Thank you for reading.

You wrote this
deck::deck(){
stack<card> decklist(card(1,1));
}
but that just declares a variable in your constructor called decklist which is different from the decklist in your class.
To initialise the decklist in your class you should use an initialiser list. Like this
deck::deck() : decklist(card(1,1)) {
}

Related

Classes & Structs, no default contructor

I'm trying to build a Linked List who's elements are of my own specified type. Now I'm not going to lie I'm winging a lot of this having not had much experience with OOP in C++ but I'm stuck with a single error.
My LinkedList:
#include "Vehicle.h"
#include "string"
using namespace std;
class LinkedList
{
private:
struct Node
{
Vehicle data;
Node* next;
};
Node* root;
int noofitems;
public:
LinkedList();
int getNoOfItems();
Vehicle getItemByIndex(int index);
void addItem(Vehicle itemIn);
void deleteItem();
void insertItem(Vehicle itemIn);
~LinkedList();
};
The Constructor and addItem()
LinkedList::LinkedList() : root(NULL), noofitems(0) {}
void LinkedList::addItem(Vehicle itemIn)
{
Node* temp;
temp = new Node();
temp->data = itemIn;
temp->next = this->root;
this->root = temp;
}
My compiler is giving me this error:
error C2512: 'LinkedList::Node' : no appropriate default constructor available. Now I've tried giving the struct a constructor like so:
struct Node
{
Vehicle data;
Node* next;
Node() : next(NULL) {}
};
But then I get a new error on top of the old one: IntelliSense: no default constructor exists for class "Vehicle". The word constructor is starting to look wrong and I'm really frustrated. Thanks In advance.
By the way if details of the vehicle class are needed:
class Vehicle
{
protected:
string make;
string model;
string regNo;
int engineSize;
bool rented;
public:
Vehicle(string makeIn, string modelIn, string regNoIn, int engineSizeIn);
string getMakeModel(); // return two values concatinated
string getRegNo();
int getEngineSize();
bool getRented();
void setRented(bool rentedIn);
~Vehicle();
};
Vehicle::Vehicle(string makeIn, string modelIn, string regNoIn, int engineSizeIn) :
make(makeIn), model(modelIn), regNo(regNoIn), engineSize(engineSizeIn),
rented(false)
{}
string Vehicle::getMakeModel()
{
return make + " " + model;
}
string Vehicle::getRegNo()
{
return regNo;
}
int Vehicle::getEngineSize()
{
return engineSize;
}
bool Vehicle::getRented()
{
return rented;
}
void Vehicle::setRented(bool rentedIn)
{
rented = rentedIn;
}
Vehicle::~Vehicle(){}
Node has a member of type Vehicle. Since you cannot default construct a Vehicle the default constructor for Node is marked as deleted. You will need to provide your own default constructor that constructs the Vehicle member to some state like
struct Node
{
Vehicle data;
Node* next;
Node() : data("", "", "", 0), next(nullptr) {}
};
or provide a default constructor for Vehicle like
class Vehicle
{
//...
public:
Vehicle() = default;
//...
};
The error is self explanatory. You have not explicitly initialized vehicle in your Node class as shown:
struct Node
{
Vehicle data;
Node* next;
Node() : next(NULL) {} // NO initialization for vehicle
};
The compiler will try and then construct a Vehicle using it's default constructor, but it finds none. In your vehicle class you have defined a constructor taking arguments:
Vehicle(string makeIn, string modelIn, string regNoIn, int engineSizeIn);
Thus the compiler will not generate one for you. To fix this you can either define a default constructor yourself, or you can declare one with the word default which will force the compiler too generate one:

Issue trying to use a template object inside a template class

Consider the following class definitions ...
Node
template <class T> class Node {
private :
T* data;
Node<T>* next;
public :
Node(T* data);
void setData(T* data);
T* getData();
void setNext(Node<T>* next);
Node<T>* getNext();
};
Linked List
template <class T> class LinkedList {
private :
Node<T>* start;
public :
LinkedList();
void add(Node<T>* node);
bool isEmpty();
};
Main
#include "Foo.h"
int main() {
Foo foo();
Node<Foo> node(&foo);
LinkedList<Foo> linkedList();
linkedList.add(&node);
return 0;
}
When it is compiled it throws the following error...
Request for member 'add' in 'linkedList', which is of non-class type 'LinkedList<Foo>()'
I am quite inexperienced making use of templates, so any help would be greatly appreciated.
Foo foo(); and LinkedList<Foo> linkedList(); are not variables but are function prototypes.
Please use Foo foo; and LinkedList<Foo> linkedList; instead.
You can also use Foo foo = Foo(); and LinkedList<Foo> linkedList = LinkedList<Foo>(); to make it clear that the constructors are called.

what is wrong with the template function parameter

Hi I am new to templates. Just want to know how to compile the program correctly.
template<class t>
class node{
public:
t val;
node(t v):val(v){}
};
template<class t>
class stack{
private:
stack *next;
static stack *head;
static int top;
public:
void push(node *n);
node* pop();
};
template<class t>
int stack<t>::top=0;
template<class t>
stack<t>* stack<t>::head=NULL;
template<class t>
void stack<t>::push(node<t>* n) //Error the push function is not defined properly
{
}
int main(int argc, char *argv[])
{
node<int> n1(5);
return 0;
}
The program gives error
stack<t>::push' : redefinition; different basic types
nw.cpp(14) : see declaration of 'stack<t>::push'
Thanks in advance
The class template node needs template arguments
Use node<t> at :
void push(node<t> *n); and node<t>* pop(); and according at implementation
The declaration:
void push(node *n);
should be:
void push(node<t> *n);
public: void push(node *n);
should be
public: void push(node<t> *n);
node is a class template so its template arguments are needed even in the declaration:
void push(node<n> *n);
node<t>* pop();
The only scenario in which you can leave the template arguments out in a parameter declaration is when the declaration appears in the class scope itself. In such a situation node is referred to as the injected class name.
Also, as the comments point out, head and top shouldn't be static data members. This inhibits the creation of independent stack instances and can cause a lot of confusion when they're being used. Instead, make them non-static data members so they only refer to the instance being used.

Using template class inside another class

I am trying to create a queue which will use generic item. I am receiving an error with the following code.
How to use the template class inside another class?
Here is what I have tried so far:
#include <iostream>
using namespace std;
template<class T>
class Item
{
public:
Item(const T & item)
: itemVal(item)
{
}
private:
T itemVal;
};
class MyQueue
{
public:
// Error #1
void InsertNode(const Item & item);
private:
struct Node {
// Error #2
Item item;
struct Node * next;
};
};
int main()
{
Item<int> * element = new Item<int>(9);
return 0;
}
Item is not a type, it is a class template. You need to provide the template parameter. In this case, int:
void InsertNode(const Item<int> & item)
and
struct Node{
Item<int> item;
Node<int> * next;
};
Otherwise, you can make MyQueue and Node class templates.
It would be better to redesign your class.
template<class T>
class MyQueue {
struct Node {
T item;
Node * next;
};
public:
MyQueue();
void InsertNode(const T & item);
private:
Node * _root;
};
P.S. Sorry for my English.

C++ template class with template class member

What is the current syntax for writing a template member class parameter inside a template class.
This is what I been trying to do:
template <class T>
class Node
{
public:
Node(); // constructor
Node(const Node<T> &); // copy constructor
~Node(); // destructor
T value;
Node *next;
};
template <class T>
class Linked_list
{
public:
Linked_list(); // constructor
Linked_list(const Linked_list<T> &); // copy constructor
~Linked_list(); // destructor
T pop();
void push(T value);
T top();
bool is_empty();
void clear();
private:
Node<T> *head; // COMPILER ERROR
};
Why this is a compiler error?
Node<T> *head; // COMPILER ERROR
Perhaps when you call:
Node<T> *head;
T is not an object type, and it doesn't know how to construct that. Try:
Node<std::string> *head;
or something like that. T is not an object type, it is just like a variable name, except it is actually a variable type within the class Node and LinkedList.