[RESOLVED] Cannot Instantiate abstract Object?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] Cannot Instantiate abstract Object?

  1. #1
    Join Date
    Dec 2008
    Posts
    5

    [RESOLVED] Cannot Instantiate abstract Object?

    Hi,

    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:

    #include "Stack.h"
    #include "Array.h"
    //#include "Iterator.h"

    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?

    Thanks,

    jesse

  2. #2
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Cannot Instantiate abstract Object?

    It is Complaining that Array<T> is abstract.

    Please be sure to:

    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
    In theory, there is no difference between theory and paractice; 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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Cannot Instantiate abstract Object?

    It's possible that Array is doing something funny. Does the same happen if you use a std::vector?

  4. #4
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Cannot Instantiate abstract Object?

    Quote Originally Posted by Lindley View Post
    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
    In theory, there is no difference between theory and paractice; 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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Cannot Instantiate abstract Object?

    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.

  6. #6
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Cannot Instantiate abstract Object?

    Quote Originally Posted by Lindley View Post
    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
    In theory, there is no difference between theory and paractice; 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

  7. #7
    Join Date
    Dec 2008
    Posts
    5

    Re: Cannot Instantiate abstract Object?

    I'm not using STL, because we're essentially re-writing portions of the STL for the class. You know, as a learning exercise.

    There's something else going on here, I'm going to dig a little deeper, then post again (this time with proper adherence to the posting guidelines)

    Thanks,

    Jesse

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Cannot Instantiate abstract Object?

    Most likely, one of the template functions for Array tries to dereference T somewhere it shouldn't. Look for that.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Cannot Instantiate abstract Object?

    Quote Originally Posted by JesseF View Post
    Hi,

    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.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Dec 2008
    Posts
    5

    Re: Cannot Instantiate abstract Object?

    1) Why is the base class "Stack" public virtual?
    It's not anymore, thanks

    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:

    StackAsArray.h:
    Code:
    #include "Stack.h"
    #include "Array.h"
    class StackAsArray: public Stack
    {
    private:
    	Array<Object*> theArray;
    public:
    	StackAsArray(unsigned int);
    	Object& Top();
    	void Push(Object&) = 0;
    	Object& Pop();
    	void Purge();
    
    	virtual ~StackAsArray();
    };
    StackAsArray.cpp:
    Code:
    #include "StackAsArray.h"
    
    StackAsArray::StackAsArray(unsigned int size)
    	: theArray(size)
    {}
    
    void StackAsArray::Purge()
    {
    	if (IsOwner())
    	{
    		for (unsigned int i=0; i<count; i++)
    			delete theArray[i];
    	}
    	count = 0;
    }
    
    StackAsArray::~StackAsArray()
    { Purge(); }
    
    void StackAsArray::Push(Object& object)
    {	
    	if (count == theArray.Length())
    		throw "Stack is full";
    	theArray[count++] = &object;
    }
    
    Object& StackAsArray::Pop()
    {
    	if(count == 0)
    		throw "Stack is empty";
    	return *theArray[--count];
    }
    
    Object& StackAsArray::Top()
    {
    	if(count == 0)
    		throw "Stack is empty";
    	return *theArray[count - 1];
    }
    Array.h:
    Code:
    #ifndef ARRAY_H
    #define ARRAY_H
    #include "object.h"
    
    template<class T>
    class Array: public Object
    {
    protected:
    	T* data;
    	unsigned int base;
    	unsigned int length;
    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);
    };
    #endif
    
    #ifndef ARRAY_SHIELD
    #include "Array.cpp";
    #endif
    Array.cpp:
    Code:
    #ifndef ARRAY_SHIELD
    #define ARRAY_SHIELD
    #include "Array.h"
    
    template<class T> Array<T>::Array()
    {
    	data = new T[0];
    	base = 0;
    	length = 0;
    }
    
    template<class T> Array<T>::Array(unsigned int array_length, unsigned int array_base = 0)
    {
    	data = new T[array_length];
    	base = array_base;
    	length = array_length;
    }
    
    template<class T> Array<T>::Array(Array<T> const& the_array)
    :	data (new T[the_array.length]),
    	base (the_array.base),
    	length (the_array.length)
    {
    	for(int i=0; i<length; i++)
    		data[i] = the_array.data[i];
    }
    
    template<class T> Array<T>::~Array()
    {	delete [] data;	}
    
    template<class T> unsigned int Array<T>::Base() const
    {	return base;	}
    
    template<class T> unsigned int Array<T>::Length() const
    {	return length;	}
    
    template<class T> T const& Array<T>::operator [](unsigned int position) const
    {
    	unsigned int const offset = position - base;
    	if(offset >= length)
    		throw out_of_range("invalid position");
    	return data[offset];
    }
    
    template<class T> T& Array<T>::operator [] (unsigned int position)
    {
    	unsigned int const offset = position - base;
    	if(offset >= length)
    		throw out_of_range("invalid position");
    	return data[offset];
    }
    
    template<class T> void Array<T>::SetBase(unsigned int new_base)
    {	base = new_base;	}
    
    template<class T> void Array<T>::SetLength(unsigned int new_length)
    {
    	T* const newData = new T[new_length];
    	unsigned int const min = 
    		length < new_length ? length: new_length;
    	for(int i=0; i<min; i++)
    		newData[i] = data[i];
    	delete [] data;
    	data = newData;
    	length = new_length;
    }
    #endif
    Stack.h:
    Code:
    #include "container.h"
    
    class Stack : public Container
    {
    public:
    	Stack();
    	virtual Object& Top() const = 0;
    	virtual void Push(Object&) = 0;
    	virtual Object& Pop() = 0;
    	
    	virtual ~Stack();
    };
    Container.h:
    Code:
    #ifndef CONTAINER_H
    #define CONTAINER_H
    
    #include "Object.h";
    #include "Ownership.h";
    
    class Container: public virtual Object,
    	public virtual Ownership
    {
    protected:
    	unsigned int count;
    	Container();
    public:
    	virtual unsigned int Count() const;
    	virtual bool IsEmpty() const;
    	virtual bool IsFull() const;
    
    	virtual void Purge() = 0;
    };
    
    #endif
    Container.cpp:
    Code:
    #include "Container.h"
    
    Container::Container(): count(0){}
    
    unsigned int Container::Count() const
    {	return count;	}
    
    bool Container::IsEmpty() const
    {	return count == 0;	}
    
    bool Container::IsFull() const
    {	return false;	}
    And finally, Object.h:
    Code:
    #ifndef OBJECT_H
    #define OBJECT_H
    
    #include <iostream>
    using std::ostream;
    
    typedef unsigned int HashValue;
    
    class Object
    {
    protected:
    
    	virtual int CompareTo(Object const&) const = 0;
    public:
    	virtual ~Object();
    	virtual bool IsNull() const;
    	virtual int Compare (Object const&) const;
    	virtual HashValue Hash() const = 0;
    	virtual void Put(ostream&) const = 0;
    };
    
    inline bool operator == (Object const& left, Object const& right)
    {	return left.Compare(right) == 0;	}
    inline bool operator != (Object const& left, Object const& right)
    {	return left.Compare(right) != 0;	}
    
    inline bool operator <= (Object const& left, Object const& right)
    {	return left.Compare(right) <= 0;	}
    inline bool operator >= (Object const& left, Object const& right)
    {	return left.Compare(right) >= 0;	}
    
    inline bool operator < (Object const& left, Object const& right)
    {	return left.Compare(right) < 0;	}
    inline bool operator > (Object const& left, Object const& right)
    {	return left.Compare(right) > 0;	}
    
    inline ostream& operator << (ostream& s, Object const& obj)
    {	
    	obj.Put(s);
    	return s;
    }
    #endif
    Attached Files Attached Files

  11. #11
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Cannot Instantiate abstract Object?

    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 03:34 AM.

  12. #12
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Cannot Instantiate abstract Object?

    Coding is from smaller part to bigger part.
    Thanks for your help.

  13. #13
    Join Date
    Dec 2008
    Posts
    5

    Re: Cannot Instantiate abstract Object?

    Paul,

    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?

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: Cannot Instantiate abstract Object?

    Quote Originally Posted by JesseF
    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

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Cannot Instantiate abstract Object?

    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.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center