Abstract_Class

Hi all I'm having trouble figuring out a return for an abstract class, please see below:

//Abstract Class
#ifndef _ITEM_H
#define _ITEM_H
#include <string>
#include <iostream>
using namespace std;

class Item
{
public:
virtual std::string AsString() = 0; //Pure Virtual
};
#endif // _ITEM_H

//Derived Class .h file
#ifndef _BOOK_H
#define _BOOK_H

#include <iostream>
#include <string>
#include "Item.h"
using namespace std;

class Book : public Item
{
private:
string _Authors;

public:
Book(string authors);
string GetAuthors();
void SetAuthors(string value);

virtual std::string AsString();
};

#endif // _BOOK_H

//Derived Class .cpp file
#include <iostream>
#include <string>
#include "Book.h"
#include "Item.h"
using namespace std;

Book::Book(string authors : Item(), _Authors(authors)
{
}

string Book::GetAuthors()
{
return _Authors;
}

void Book::SetAuthors(string value)
{
_Authors = value;
}

virtual std::string AsString()
{
return _Authors; /*I get an undeclared identifier error here, if you can help it would be greatly appreciated.*/


}

Last edited on
the problem is you havent declared _Title, because if you check your class definition it only says

1
2
private:
     string _Authors;


so that's why your compiler is complaining that it doesn't know what _Title is, or where to get it.
Thanks Dacster but I copied it across incorrectly, I cut out some of the code to shorten the code. It it should have been:

virtual std::string AsString()
{
return _Authors; /*I get an undeclared identifier error here, if you can help it would be greatly appreciated.*/


}
Typically you don't put "virtual" with the function body.

You also need the scope operator since you're not in the class body anymore.

IE:

1
2
3
4
std::string Book::AsString() // note:  scope operator.  No need for "virtual"
{
  return _Authors;  // no more error here
}


The problem is the compiler is making AsString a global function because you don't give it the Book scope. Since there's no global _Authors, that's why you get that error.
Thank you, that's a great help :)
Topic archived. No new replies allowed.