2

Just recently learned the correct application of inheritance of classes in Java and thought to apply it to some code I'm writing in C++.

The following is a header for an empty Node.cpp:

#ifndef Node_h
#define Node_h

#include <string>

using namespace std;

///////////////////////////////////////////////////////
//Class Declarations
class Array;
class Hash;
class Node;
    class BodyNode;
        class RealNode;
            class CharNode;
            class ComboNode;
        class EndNode;
    class HashNode;
    class NodeFollower;

///////////////////////////////////////////////////////
//Class Definitions
class Node{
public:
    virtual string toString(int info) = 0;
    };

class BodyNode: public Node{
public:
    };

class RealNode: public BodyNode{
protected:
    Hash *wordHash, *addressHash;
    Array *followers;
    int count;

//Initialization
    RealNode();
    ~RealNode();

//Hashes
    BodyNode getNext(int address);
    NodeFollower getWord(BodyNode *word);
    NodeFollower getAddress(int address);

    string toString(int info);
    };

#endif

From this code, I get the following error:

Node.h:43:11: error: invalid abstract return type for member function ‘BodyNode RealNode::getNext(int)’
Node.h:28:7: note:   because the following virtual functions are pure within ‘BodyNode’:
Node.h:25:17: note:     virtual std::string Node::toString(int)

Looked this up all over google, stackoverflow, and cplusplus, and while I find a lot of answers that surround this concern, I couldn't find the exact answer that I needed to fix it. The cplusplus.com site that I was sourcing from is:

http://www.cplusplus.com/doc/tutorial/polymorphism/

Additionally, a few of the answers I found on stackoverflow indicated that

using namespace std;

is to be avoided, and it sounds like for good reason, but I can't figure out how to include string. I tried

string::string

for each declaration, and I tried

using namespace std::string;

but both were to no avail. Thank you in advance for the help!

1
  • 1
    if you don't want to use the whole namespace you will have to qualify it wherever you use it. So it will be std::string variableName; I'd suggest you read what namespaces are :) and how they work ! Commented Feb 20, 2012 at 12:29

4 Answers 4

3

Your first problem is that BodyNode inherits from regular Node which has a toString() pure-virtual function. If it's pure-virtual (the =0) then you MUST provide a definition for it in all derived classes, and you have not.

So, since you have not provided the pure-virtual function definition within BodyNode, it is also an abstract class and uninstantiable (an abstract-class has 1 or more pure-virtual methods so it cannot be instantiated).

You also have an issue that getNext() returns a BodyNode type, and since BodyNode is abstract, this isn't possible - you COULD return a pointer to it instead of a by-copy-value, but I think your real problem is that it shouldn't be abstract in the first place.

Provide a default toString() function in BodyNode to fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Ohhhhh... Thank You! Wow, I never would have gotten that!
2

You cannot use abstract class as a function return type. But you can return pointer or reference to it, like in following declarations:

BodyNode* getNext(int address);
BodyNode& getNext(int address);

You can declare your string as:

#include <string>
...
std::string myString;

2 Comments

Thank you for your assistance. w00te was able to piece together what I was actually trying to accomplish, but thank you very much as well!
@Daniel You are welcome. Glad you have found the solution to the problem.
0

Since BodyNode is also an abstract class, you can not return it from getNext. You either have to return a reference or a pointer.

2 Comments

My goal here is to not have BodyNode as an abstract class. I would like Node to be abstract, and I would like BodyNode to use virtual functions, (which had toString() defined, but isn't included here and seemed to have no impact on the error) and I would like RealNode to be a "solid" object.
@DanielP. In that case you have to change BodyNode from being an abstract class, by implementing toString in that one too.
0

BodyNode is also an abstract class because - Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes.

You can not return an object of type BodyNode. You can only return a pointer or a reference. Refer this-

http://msdn.microsoft.com/en-us/library/c8whxhf1.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.