Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:38:08+00:00 2026-06-15T14:38:08+00:00

So I have an abstract base class, Collection. I understand that it is abstract

  • 0

So I have an abstract base class, Collection. I understand that it is abstract because it declares at least one pure virtual function.

I have a subclass of Collection, OrderedCollection, which declares those functions exactly in it’s own header file. I then define those exact same functions in OrderedCollection’s Source file.

Here is the code:

Class Collection.h

class Collection {

public:
virtual Collection& add(int i, int index) = 0;  //pure virtual
virtual Collection& remove(int i) = 0;  //pure virtual
virtual Collection& operator=(const Collection& other)=0;   //pure virtual
virtual int& operator[](int i) = 0; //pure virtual

Collection& iterate(void (*pFunc)());       //Function takes pointer to     function as argument
bool contains(int i);

virtual Collection& copy();


virtual ~Collection();

int size() { return size; }
int capacity() { return capacity; }

//virtual void swap(Collection& other);

protected:
Collection(){}

std::vector collectionVec;
int size;
int capacity;
};

Derived class OrderedCollection.h:

class OrderedCollection : public Collection
{
public:
OrderedCollection& add(int i, int index);
OrderedCollection& remove(int i);
OrderedCollection& operator=(const OrderedCollection& other);
int& operator[](int i);

OrderedCollection();
//OrderedCollection::OrderedCollection(int pFirst, int pLast, int pSize, int     pCapacity, std::vector<int> passedVec);

//OrderedCollection(const OrderedCollection& other);    //Copy constructor 

virtual ~OrderedCollection();

virtual OrderedCollection& copy(OrderedCollection& passedCollection);

protected:
//int* first;
//int* last;

int first;
int last;

OrderedCollection& grow();      //Utility Function
};

aaand OrderedCollection.cpp:

#include "OrderedCollection.h"

OrderedCollection& OrderedCollection::add(int i, int index){
if(size == capacity){       //If vector is full

}

return *this;
}

OrderedCollection& OrderedCollection::remove(int i){

if(first <= last){
    for(int j = first; j <= last; j++){
        if(collectionVec.at(j) == i){
            collectionVec.erase(j);
            last--;
        }
    }
}
/*for(int j = 0; j < collectionVec.size(); j++){
if(collectionVec.at(j) == i)

} */

return *this;
 }

OrderedCollection& OrderedCollection::operator=(const OrderedCollection& other){


if (this != &other) // protect against invalid self-assignment
{
    // 1: allocate new memory and copy the elements
    std::vector<int> *new_vector = new std::vector<int>(other.capacity);
    std::copy(other.collectionVec, other.collectionVec + other.capacity,     new_vector);

    // 2: deallocate old memory
    collectionVec.clear();


    // 3: assign the new memory to the object
    collectionVec = *new_vector;
    size = other.size;      //wtf
    capacity = other.capacity;      //wtf

    delete new_vector;
}
// by convention, always return *this
return *this;


}

int& OrderedCollection::operator[](int i){      //is return type correct? It makes     more sense to have a return type of int or int&, right?

int temp = 0;

if(first <= last){
    if(collectionVec.at(first + i) != NULL){    //Need to redo this
        return collectionVec.at(first + i);
    }
}
return temp;
}

OrderedCollection::OrderedCollection() : Collection()
{
//first = &collectionVec.at(2);
//last = &collectionVec.at(1);

//Crossed at construction
first = 2;
last = 1;
}



OrderedCollection::~OrderedCollection(void)
{
//first = NULL;
//last = NULL;
collectionVec.clear();
}

OrderedCollection& OrderedCollection::grow(){

    int newFirst = capacity / 2;
    std::vector<int> *new_vector = new std::vector<int>(2 * capacity);
    std::copy(collectionVec, collectionVec+size, new_vector->begin() + newFirst);       //Want to return iterator pointing to 
    collectionVec = new_vector;
    first = newFirst;
    last = first + size;
    capacity = collectionVec.size;

    delete new_vector;

    return *this;
}

OrderedCollection& OrderedCollection::copy(OrderedCollection& passedCollection){
OrderedCollection* temp =  new OrderedCollection()   //ERROR is here. VS highlights constructor method

return *this;
}

Now the issue comes when I am trying to create either a value identifier of type OrderedCollection within this last copy() here. As I understand it, I shouldn’t be allowed to do this if the class is abstract (so clearly it is abstract, also VS tells me so). But there is another issue; I get the same error when I try to create a new OrderedCollection object and assigning it to temp. The above intialization is fine according VS (no complaints from the IDE, although it doesnt help me any). But I can’t figure out why it’s regarding this class as abstract.

Correct me if I’m wrong, but this should cover all of the bases in making sure that this derived class is NOT abstract..

  • There are no pure virtual functions declared within the derived class
  • All functions that were pure virtual in the base class, have been overridden in the derived class.
  • All of the overridden functions match the relevant function argument signature & return type, as originally declared in the base class.

The error exactly is, Error: object of abstract class type “OrderedCollection” is not allowed… And this is when I try to assign a pointer object to a new instance of OrderedCollection within this copy() method at the bottom.

Let me post my Collection.cpp file below:

#include "Collection.h"


Collection::Collection()
{
size = 0;
capacity = 4;
collectionVec.resize(capacity);
}


Collection::~Collection()
{

}

Collection& Collection::iterate(void (*pFunc)()){       

return *this;
}


bool contains(int i){


return true;
}

Edit: Added Collection.cpp file and updated some fixes I’ve made in regards to mismatching function parameters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:38:09+00:00Added an answer on June 15, 2026 at 2:38 pm
    virtual Collection& operator=(const Collection& other)=0;
    

    is not being overridden in the child class, because:

    OrderedCollection& operator=(OrderedCollection& other);
    

    Has a different signature.

    The difference is not just the ‘const’, but the actual type. The overridden class must also take a Collection, and not the derived OrderedCollection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class and want to implement a function in the
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
I have a abstract base C# class with a couple of methods that has
I have two classes (MVC view model) which inherits from one abstract base class.
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have a collection of classes that inherit from an abstract class I created.
Ok this one is puzzling me. I have an abstract base class named Testbase
I have a base class that has an abstract method which returns a list
I have a base object abstract class and a base object collection class for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.