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

  • Home
  • SEARCH
  • 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 6147757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:09:31+00:00 2026-05-23T19:09:31+00:00

So I have a vector of pointers holding a baseclass. I create two elements

  • 0

So I have a vector of pointers holding a baseclass. I create two elements inside the vector and then attempt to swap them after a few layers of abstraction. Currently this causes the compiler to explode and throw several errors inside move.h that look like this:

*c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/move.h: In function 'void std::swap(_Tp&, _Tp&) [with _Tp = Base]':
D:\My Documents\pointertest2\main.cpp:52:   instantiated from here
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/move.h:81: error: cannot allocate an object of abstract type 'Base'
D:\My Documents\pointertest2\main.cpp:7: note:   because the following virtual functions are pure within 'Base':
D:\My Documents\pointertest2\main.cpp:11: note:     virtual int Base::GetInt()
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/move.h:81: error: cannot declare variable '__tmp' to be of abstract type 'Base'
D:\My Documents\pointertest2\main.cpp:7: note:   since type 'Base' has pure virtual functions*

The code that causes this problem is as follows:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class Base {
  public:

    virtual int GetInt() = 0;
    int a;
};

class Test : public Base {
  public:

    int GetInt()
    {
        return a;
    }
};

class Slot {
  public:

    Base *connected;
};

int main()
{
    std::vector<Base*> testVec;

    Base *test = new Test;
    testVec.push_back(test);
    testVec[0]->a = 1;

    Base *test2 = new Test;
    testVec.push_back(test2);
    testVec[1]->a = 2;

    Slot slot;
    slot.connected = testVec[0];

    Slot slot2;
    slot2.connected = testVec[1];

    Slot* slottemp = &slot;
    Slot* slottemp2 = &slot2;

    std::swap(*slottemp->connected, *slottemp2->connected);

    cout << testVec[0]->GetInt() << endl;
    cout << testVec[1]->GetInt() << endl;

    return 0;
}

You can see at the end i’m hoping that testVec[0] will return 2, and testVec[1] will return 1, as these are the swapped values i’m looking for.

Any help would be greatly appreciated, my head has been exploding on this one. I’m entirely open to alternate methods of swapping the pointers contained as elements 0 and 1, this is just where i’ve ended up at so far.

  • 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-05-23T19:09:33+00:00Added an answer on May 23, 2026 at 7:09 pm

    You’re trying to swap objects of type Base.

    slottemp->connected is of type Base *, so by swapping *slottemp->connected, you’re swapping two Base &. Since there’s no std::swap overload for Base &, the compiler falls back to the default, which goes something like this:

    template <class T> void swap ( T& a, T& b )
    {
      T c(a); a=b; b=c;
    }
    

    If we instantiate this on T = Base, then we try to construct a temporary Base for the swap; this fails, because Base has pure virtual functions.

    One option is to write a std::swap overload for Base:

    namespace std {
    void swap(Base &a, Base &b) {
      std::swap(a.a, b.a);
    }
    }
    

    This will allow you to swap the underlying Bases. However, this is unlikely to be well-suited to a virtual base class – what happens if someone tries to swap a Foo and a Bar, both derived from Base?

    If you just want the Slots swapped, you can swap the Base *s, not the Bases themselves:

    std::swap(slottemp->connected, slottemp2->connected);
    

    If you want the changes to be reflected in the vector, you should make Slot hold an iterator, or a pointer to the pointer inside the vector

    class Slot {
      public:
        Base **connected;
    };
    
    // ...
    slot.connected = &testVec[0];
    // ...
    
    std::swap(*slot.connected, ...);
    

    In this particular case, you know the true type is Test, so you can also cast to this concrete type and swap that way:

    std::swap(*(Test *)slottemp->connected, *(Test *)slottemp2->connected);
    

    I wouldn’t recommend this approach, as it invites the possibility of slicing (or worse, illegal casting) later if you start adding values of type other than Test to the vector.

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

Sidebar

Related Questions

I have a set of objects in a vector of pointers to their baseclass
Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
I have a vector of pointers that are not owned by the container. How
I have a vector of pointers to a class. I need to call their
I have a vector of pointers to objects. I'd like to remove objects from
I have a vector of pointers. I would like to call a function for
I have a vector of pointers to Mouse objects called 'mice'. I'm passing the
I have a tree of objects, where each object has a std::vector of pointers
Suppose I have a std::vector<Obj *> objs (for performance reasons I have pointers not
I have a vector with pointers of type Vehicle. Vehicle is the base class

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.