Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Abstract classes calling abstract methods
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Wed Mar 11, 2015 11:26 am   Post subject: Abstract classes calling abstract methods

c:

#ifndef PIRATE_H
#define PIRATE_H

#include "defs.h"

class Pirate {
        private:
                // Represents the ID for the next pirate generated
                static int nextID;

                // ID of Pirate
                int id;
                // Space used by Pirate
                int space;

        protected:
                virtual int generateSpace() = 0;

        public:
                Pirate();

                // Returns Pirate's ID
                int getID();
                // Returns space used by Pirate
                int getSpace();

                // Returns next ID and generates the next one
                static int getNextID();
};

#endif // PIRATE_H


c:


#include "Pirate.h"

int Pirate::nextID = 1001;

// Randomly generated size and procedurally generated ID
Pirate::Pirate() {
        this->id = getNextID();
        this->space = generateSpace(); // THIS IS GIVING ME GRIEF
}

// Returns pirate's ID
int Pirate::getID() {
        return id;
}

// Returns pirate's space used
int Pirate::getSpace() {
        return space;
}

// Returns next ID, and generates a new one.
int Pirate::getNextID() {
        return (nextID++);
}


This results as an error on the aforementioned line : "Pirate.o:Pirate.cc:(.text+0x23): undefined reference to `Pirate::generateSpace()'"

In java, you can do stuff like this. Is there a way for me to be able to call a pure virtual function in my abstract class?
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Wed Mar 11, 2015 12:59 pm   Post subject: RE:Abstract classes calling abstract methods

Gonna take a guess, but does defining an actual function instead of assigning it as 0 work? It feels like you're assigning a pointer value of 0, possibly to the function's body, which is causing it hiccup.

Eg (I have no idea if this is correct syntax):

c++:

virtual inline int generateSpace() { return 0; }


Also, there's the inline keyword which I noticed in a project I contribute to, though their inline functions weren't virtual.
Dreadnought




PostPosted: Wed Mar 11, 2015 2:48 pm   Post subject: Re: Abstract classes calling abstract methods

I believe the problem is that the parent class' constructor is called before the child object's constructor is, making it unclear which subclass' version of the virtual function to call. Suppose AngryPirate is a subclass of Pirate, then creating a new AngryPirate will first create the Pirate part of the object and then create the AngryPirate part. If my guess is correct, your virtual function, which is being called during the Pirate's constructor, doesn't know that it's supposed to call the AngryPirate constructor, because the object is still only a Pirate.

Better explanation:
http://stackoverflow.com/questions/3091833/calling-member-functions-from-a-constructor

Basically, you shouldn't call virtual functions from the constructor, instead just create different constructors for the subclasses.

edit: spelling/explanation
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: