I have wrote out a simple data structure and can't seem to figure out where to place the include statement for one of my classes. The compiler seems not to recognize me creating my object because I've placed it wrong. I'm in the process of debugging my code so there are many other errors which I'm still working on. If you would like to make a comment on things that's incorrect or better coding practice it is of course appreciated. However, the real reason I post this code is to correct the issue I've stated before.
element.h
Code:
#ifndef ELEMENT_H_INCLUDED
#define ELEMENT_H_INCLUDED
class Element{
private:
int number;
Element *next = new Element(NULL);
Element *prev = new Element(NULL);
public:
Element(int number); //Constructor
~Element(); //Destructor
int getNumber(); //Return number
void setNext(Element e);
void setPrev(Element p);
Element getPrev();
Element getNext();
};
#endif // ELEMENT_H_INCLUDED
element.cpp
Code:
#include "Element.h"
#include <cstddef>
//Constructor
Element::Element(int number){
this.number = number;
this.next = NULL;
this.prev = NULL;
}
~Element::Element(){
std::cout << "Element Detroyed" << std::endl;
}
int Element::getNumber(){
return this.number;
}
void Element::setNext(Element e){
this.next = e;
}
void Element::setPrev(Element e){
this.prev = e;
}
Element Element::getPrev(){
return this.prev;
}
Element Element::getNext(){
return this.next;
}
customstack.h
Code:
#ifndef CUSTOMSTACK_H_INCLUDED
#define CUSTOMSTACK_H_INCLUDED
class customstack{
private:
int size;
Element *traverseElement = new Element(-1);
public:
customstack(); //Constructor
~customstack(); //Destructor
void push(int number);
int pop();
int getSize();
};
#endif // CUSTOMSTACK_H_INCLUDED
customstack.cpp
Code:
#include "Element.h"
#include <cstddef>
#include "customstack.h"
//Constructor
customstack::customstack(){
this.size = 0;
this->traverseElement.setNext(NULL);
this->traverseElement.setPrev(NULL);
}
~customstack::customstack(){
//destroyed
}
void customstack::push(int number){
Element *temp = new Element(number);
temp->setPrev(traverseElement);
temp->setNext(NULL);
this->traverseElement.setNext(temp);
this->traverseElement = temp;
this.size++;
}
int customstack::pop(){
int temp = this->traverseElement.getNumber();
Element *t = new Element(NULL);
t = this.traverseElement;
this->traverseElement = this->traverseElement.getPrev();
this->traverseElement.setNext(NULL);
delete t;
this.size--;
return temp;
}
int customstack::getSize(){
return this.size;
}
mainclient.cpp
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "customstack.h"
using namespace std;
int main(){
customstack *myStack = new customstack;
myStack->push(45);
srand(unsigned time(0));
myStack->push(rand()%4000 + 1);
for(int i = 0; i < 20; i++){
myStack->push(rand() % 4000 + 1);
}
myStack->getSize();
return 0;
}
compiler error
Code:
In file included from mainclient.cpp:4:0:
customstack.h:8:9: error: ‘Element’ does not name a type <~~ Error I need help with
mainclient.cpp: In function ‘int main()’:
mainclient.cpp:11:11: error: expected primary-expression before ‘unsigned’
In file included from Element.cpp:1:0:
Element.h:8:41: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
Element.h:9:41: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
Element.h:8:37: error: ‘NULL’ was not declared in this scope
Element.h:9:37: error: ‘NULL’ was not declared in this scope
Element.cpp: In constructor ‘Element::Element(int)’:
Element.cpp:7:10: error: request for member ‘number’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp:8:10: error: request for member ‘next’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp:9:10: error: request for member ‘prev’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp: At global scope:
Element.cpp:12:1: error: invalid use of destructor ‘~Element’ as a type
Element.cpp: In member function ‘int Element::getNumber()’:
Element.cpp:17:17: error: request for member ‘number’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp: In member function ‘void Element::setNext(Element)’:
Element.cpp:21:10: error: request for member ‘next’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp: In member function ‘void Element::setPrev(Element)’:
Element.cpp:25:10: error: request for member ‘prev’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp: In member function ‘Element Element::getPrev()’:
Element.cpp:29:17: error: request for member ‘prev’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
Element.cpp: In member function ‘Element Element::getNext()’:
Element.cpp:33:17: error: request for member ‘next’ in ‘this’, which is of pointer type ‘Element* const’ (maybe you meant to use ‘->’ ?)
In file included from customstack.cpp:1:0:
Element.h:8:41: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
Element.h:9:41: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
Element.h:8:37: error: ‘NULL’ was not declared in this scope
Element.h:9:37: error: ‘NULL’ was not declared in this scope
In file included from customstack.cpp:3:0:
customstack.h:8:50: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
customstack.cpp: In constructor ‘customstack::customstack()’:
customstack.cpp:8:10: error: request for member ‘size’ in ‘this’, which is of pointer type ‘customstack* const’ (maybe you meant to use ‘->’ ?)
customstack.cpp:9:27: error: request for member ‘setNext’ in ‘((customstack*)this)->customstack::traverseElement’, which is of pointer type ‘Element*’ (maybe you meant to use ‘->’ ?)
customstack.cpp:10:27: error: request for member ‘setPrev’ in ‘((customstack*)this)->customstack::traverseElement’, which is of pointer type ‘Element*’ (maybe you meant to use ‘->’ ?)
customstack.cpp: At global scope:
customstack.cpp:13:1: error: invalid use of destructor ‘~customstack’ as a type
customstack.cpp: In member function ‘void customstack::push(int)’:
customstack.cpp:19:34: error: invalid conversion from ‘Element*’ to ‘int’ [-fpermissive]
In file included from customstack.cpp:1:0:
Element.h:12:9: error: initializing argument 1 of ‘Element::Element(int)’ [-fpermissive]
customstack.cpp:20:23: warning: passing NULL to non-pointer argument 1 of ‘Element::Element(int)’ [-Wconversion-null]
customstack.cpp:21:27: error: request for member ‘setNext’ in ‘((customstack*)this)->customstack::traverseElement’, which is of pointer type ‘Element*’ (maybe you meant to use ‘->’ ?)
customstack.cpp:23:10: error: request for member ‘size’ in ‘this’, which is of pointer type ‘customstack* const’ (maybe you meant to use ‘->’ ?)
customstack.cpp: In member function ‘int customstack::pop()’:
customstack.cpp:27:38: error: request for member ‘getNumber’ in ‘((customstack*)this)->customstack::traverseElement’, which is of pointer type ‘Element*’ (maybe you meant to use ‘->’ ?)
customstack.cpp:28:34: warning: passing NULL to non-pointer argument 1 of ‘Element::Element(int)’ [-Wconversion-null]
customstack.cpp:29:14: error: request for member ‘traverseElement’ in ‘this’, which is of pointer type ‘customstack* const’ (maybe you meant to use ‘->’ ?)
customstack.cpp:30:51: error: request for member ‘getPrev’ in ‘((customstack*)this)->customstack::traverseElement’, which is of pointer type ‘Element*’ (maybe you meant to use ‘->’ ?)
customstack.cpp:31:27: error: request for member ‘setNext’ in ‘((customstack*)this)->customstack::traverseElement’, which is of pointer type ‘Element*’ (maybe you meant to use ‘->’ ?)
customstack.cpp:33:10: error: request for member ‘size’ in ‘this’, which is of pointer type ‘customstack* const’ (maybe you meant to use ‘->’ ?)
customstack.cpp: In member function ‘int customstack::getSize()’:
customstack.cpp:38:17: error: request for member ‘size’ in ‘this’, which is of pointer type ‘customstack* const’ (maybe you meant to use ‘->’ ?)