I'm working on a project for school, and I'm getting an error that doesn't make sense to me. Here's the code where the error comes from, with the error inserted to the side:
class StackAsArray: public virtual Stack
{
private:
Array<Object*> theArray; <---------- Error C2259:
Array<T> cannot instantiate abstract class
class Iter;
public:
StackAsArray(unsigned int);
Object& Top();
void Push(Object&) = 0;
Object& Pop();
void Purge();
virtual ~StackAsArray();
friend class Iter;
};
Object is, in fact, abstract, but I'm not instantiating the object, just a pointer to it. Object is the base class from which all other classes derive from, and I want to be able to store an array of them, regardless of specific type. The above code snippet is actually the solution suggested in my textbook. What am I missing? Is there a better way?
1) ALWAYS use code tags. Go back and re-read the "Before you Post FAQ".
2) ALWAYS post minimal yest complete code, this means the smallest possible code sample that can be compiled and reproduce the problem.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009,2010 In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
It's possible that Array is doing something funny. Does the same happen if you use a std::vector?
Note that the OP is NOT using STL...
Code:
#include "Stack.h"
#include "Array.h"
if this WERE STL...
Code:
#include <Stack>
#include <Array>
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009,2010 In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
I know, hence why I said it might be doing something funny. One can intuit the usage from the name though.
Although there is no <array> header in STL, of course.
OF Course (it would most likely be <List> or <Vector>)
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009,2010 In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
I'm working on a project for school, and I'm getting an error that doesn't make sense to me. Here's the code where the error comes from, with the error inserted to the side:
1) Why is the base class "Stack" public virtual?
The above code snippet is actually the solution suggested in my textbook. What am I missing?
2) What is "array.h"? "stack.h"? They are important for us to see them, so as to know exactly what you're trying to compile.
2) What is "array.h"? "stack.h"? They are important for us to see them, so as to know exactly what you're trying to compile.
I had left those bits out for the sake of brevity, because I thought the issue was with the "Object*" portion, but it doesn't seem to be, as I can declare an Object* by itself without generating an error. Below is all the code required to run this piece, in case you don't want to. I've also attached the files, if you plan to compile them. Enjoy:
What compiler are you using (compiler, version)? This should not have compiled:
Code:
template<class T> Array<T>::Array(unsigned int array_length, unsigned int array_base = 0)
You're not allowed to use default arguments if the function has already been declared using a default argument.
You have other errors, such as "out_of_range" not being defined. I am attempting to take your code and compile it using the Comeau on-line compiler, and these are the errors that it's coming up with. If you had whittled this down to a smaller program, the error is obvious:
Code:
#include <iostream>
using std::ostream;
typedef unsigned int HashValue;
class Object
{
protected:
virtual int CompareTo(Object const&) const = 0;
public:
virtual HashValue Hash() const = 0;
virtual void Put(ostream&) const = 0;
};
template<class T>
class Array: public Object
{
protected:
public:
Array();
Array(unsigned int, unsigned int =0);
~Array();
Array(Array const&);
T const& operator [](unsigned int) const;
T& operator [] (unsigned int);
T const& Data() const;
unsigned int Base() const;
unsigned int Length() const;
void SetBase(unsigned int);
void SetLength(unsigned int);
};
int main()
{
Array<Object*> a;
}
Where is Hash(), CompareTo(), and Put() implemented in Array? You can't instantiate an abstract class without an implementation in the derived classes of the pure virtual functions.
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 39: error: object of abstract class type "Array<Object *>" is
not allowed:
pure virtual function "Object::CompareTo" has no overrider
pure virtual function "Object::Hash" has no overrider
pure virtual function "Object::Put" has no overrider
Array<Object*> a;
^
1 error detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Secondly, it sure seems like whoever is teaching you this is a Java programmer who is trying to make C++ look like Java (which is a huge waste of time and resources). The idea of a "god" object is a dead-giveaway. What exactly is the name of the class you're taking?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 8th, 2008 at 04:34 AM.
Thanks for the help. I guess I've been staring at my code for too long to notice the obvious
I'm compiling using .NET 3.5, not getting the other errors you mentioned (although I have before). It's possible the compiler was spitting out after the first problem, and it just hadn't made it to the rest of the issues yet. I'm going back and implementing those functions (and fixing the excess) now, hopefully I can mark this SOLVED later today
This is a standard Data Structures class, we're using a book by Bruno Preiss. The idea of a "god object" makes sense to me when you're talking about being able to treat multiple structures and data containers the same way. I take it there's a better way? What do you suggest?
This is a standard Data Structures class, we're using a book by Bruno Preiss. The idea of a "god object" makes sense to me when you're talking about being able to treat multiple structures and data containers the same way. I take it there's a better way? What do you suggest?
We typically use templates instead in C++.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
I seem to recall there being some kind of problem using default parameters on templated functions. I'm not sure if that's actually forbidden or just tough to get right, though.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.