Thread: Issue with placements of includes.

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    16

    Issue with placements of includes.

    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 ‘->’ ?)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Element *traverseElement = new Element(-1);
    Try putting the assignment in the constructor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    16
    Why can I declare an integer buy not instantiate an object?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by seanrsolutions
    Why can I declare an integer buy not instantiate an object?
    It is a matter of context. If you are compiling with respect to the 2003 edition of the C++ standard or earlier, then at that place in the class definition, you can only declare the member variables, not initialise them. If you are compiling with respect to the 2011 edition of the C++ standard, then you can do that, but your constructor has an int parameter, yet you passed NULL as an argument (NULL may indeed be an nt, e.g., 0, but it is a null pointer constant and should be treated as such).

    So, what you can do is to define the constructor to initialise those member pointers instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You seem to be using this.some_member a lot. You can't do that. In C++, "this" is a pointer, so you have to use this->some_member. That will clear up most of your errors. Then you can focus on the others.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    16
    Yeah that seems to be an issue I'm having now.

    Code:
    #include "Element.h"
    #include <cstddef>
    #include <iostream>
    
    
    //Constructor
    Element::Element(int number){
        this->number = number;
        Element *next = new Element(NULL);
        Element *prev = new Element(NULL);
    }
    
    Element::~Element(){
        std::cout << "Element Detroyed" << std::endl;
    }
    
    int Element::getNumber(){
        return this->number;
    }
    
    void Element::setNext(Element e){
        this->next = e;                                   //Element.cpp:22:12: error: ‘next’ was not declared in this scope
    }
    
    void Element::setPrev(Element e){
        this->prev = e;
    }
    
    Element Element::getPrev(){
        return this->prev;
    }
    
    Element Element::getNext(){
        return this->next;
    }
    So if 'this' is a pointer and 'next' is how do both get dereferenced.
    Tried a few ways. Haven't figured it out yet.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    in setNext and setPrev, you're passing values to the functions, and expecting to assign them to pointers. That's not going to work. Make the function parameter a pointer in each case. Also, getNext and getPrev both return values. Change them to return pointers.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Jun 2014
    Posts
    16
    Quote Originally Posted by Elkvis View Post
    in setNext and setPrev, you're passing values to the functions, and expecting to assign them to pointers. That's not going to work. Make the function parameter a pointer in each case. Also, getNext and getPrev both return values. Change them to return pointers.
    Thank you. After fixing there still seems to be a problem with getting to the "next" and "prev" pointer. "this" is a pointer so I have to dereference it and get to "next" with -> but "next" is also a pointer but if the function perameter that I am using is a pointer than I dont see a problem. However, now I'm getting an "next was not declared in this scope" error.

    Code:
    #include "Element.h"
    #include <cstddef>
    #include <iostream>
    
    
    //Constructor
    Element::Element(int number){
        this->number = number;
        Element* next = new Element(NULL);
        Element* prev = new Element(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;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong:
    Code:
    Element::Element(int number){
        this->number = number;
        Element* next = new Element(NULL);
        Element* prev = new Element(NULL);
    }
    next and prev are member variables, so you should not be re-declaring them in the body of the constructor. As I noted earlier, you should not pass NULL as an argument to the Element constructor because the parameter is an int, whereas NULL is a null pointer constant. Worse still, you are recursively invoking the constructor for Element, but there is no base case, i.e., you have infinite recursion. You probably want to initialise them to be null pointers instead, so use the initialiser list:
    Code:
    Element::Element(int number_) : number(number_), next(NULL), prev(NULL) {}
    I have taken the liberty of decorating the parameter name with a trailing underscore to differentiate it from the member named number. This is not absolutely necessary. I gather that you are not compiling with respect to C++11, so I have used NULL. If you were compiling with respect to C++11, use nullptr.

    Quote Originally Posted by seanrsolutions
    After fixing there still seems to be a problem with getting to the "next" and "prev" pointer. "this" is a pointer so I have to dereference it and get to "next" with -> but "next" is also a pointer but if the function perameter that I am using is a pointer than I dont see a problem. However, now I'm getting an "next was not declared in this scope" error.
    What exactly is the code that resulted in this error?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from issues already mentioned, the problem that's preventing compiling is that instantiating a class is not possible unless the class is defined previously within the translation unit.

    customstack.h is attempting to instantiate Element (assuming compiling as C++11) but Element is not even declared within it. It is therefore not possible to #include customstack.h unless element.h has been previously #include'd. The choices to fix this are

    1) #include "element.h" within customstack.h to get that to compile. Otherwise any source file that #include's customstack.h without previously #include'ing element.h cannot compile.
    2) Add a forward declaration of Element to customstack.h and move the initialisation of traverseElement to customstack's constructor. This eliminates the need to #include "element.h" within customstack.h but any translation units that need both Element and CustomStack to be defined and not just declared (for example, instantiating both of them, passing them by value, etc) will need to #include both.

    The second will work for any C++ compiler. The first will only work for C++-11 compilers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Jun 2014
    Posts
    16
    Thank all of you guys so much. It is much appreciated. I have everything working great. I am going to study up more on the differences between the dot operator and the arrow and how to properly use header files. I felt I understood however now I feel I need more clarification. I have fixed all errors in my code but running valgrind find some memory leaks but that is an easy fix. I would like to post all my corrections so I may feel like this thread is completed and solved but for some reason the "code" tags keep deleting when I press post and I get an error and suggestion about reading the rules and to use the code tags lol. I'll try to post it in a few hours. Thank you all again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shuffling order of placements
    By corrineseeley in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2010, 06:47 PM
  2. who includes who?
    By sept in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2008, 06:53 AM
  3. Different includes
    By Ganoosh in forum C++ Programming
    Replies: 10
    Last Post: 06-22-2005, 01:52 PM
  4. Includes
    By golfinguy4 in forum Windows Programming
    Replies: 7
    Last Post: 01-29-2002, 05:29 PM
  5. #includes
    By RedLenses in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2002, 03:59 PM

Tags for this Thread