0

abstract base class:

#ifndef BUILDINGORG_H
#define BUILDINGORG_H
#include <iostream>
#include <memory> 
#include <vector>

class BuildingOrg
{

public:
   BuildingOrg(int _id);
   virtual int addBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg,
                                    std::string _type) const;
   virtual void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg);
   virtual void getInfo()=0;
private:
   int id;
   std::string type;
};

#endif // BUILDINGORG_H

concrete subclass:

#ifndef BUILDINGCOMPONENT_H
#define BUILDINGCOMPONENT_H
#include "buildingorg.h"

class BuildingComponent : public BuildingOrg
{
public:
    BuildingComponent(int _id);
    int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg,
                             std::string _type) const override;
    void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
                                                        override;
    void getInfo() override;
private:
    std::vector<std::shared_ptr<BuildingOrg>> building_Org;
};

#endif // BUILDINGCOMPONENT_H

Implementation of subclass:

#include "buildingcomponent.h"

BuildingComponent::BuildingComponent(int _id):
BuildingOrg(_id)
{
}

int BuildingComponent::addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const
{
    building_Org.push_back(_buildingOrg);// I am having error here
    return 1;
}

void BuildingComponent::removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
{

}

void BuildingComponent::getInfo()
{

}

When I try to put shared pointer in my Vector I get this nasty error; I really don't know why I am getting the error:

cpp:10: error: passing 'const std::vector<std::shared_ptr<BuildingOrg> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::shared_ptr<BuildingOrg>; _Alloc = std::allocator<std::shared_ptr<BuildingOrg> >; std::vector<_Tp, _Alloc>::value_type = std::shared_ptr<BuildingOrg>]' discards qualifiers [-fpermissive]
        building_Org.push_back(_buildingOrg);

I don’t understand what is it saying.

0

3 Answers 3

4

The const in int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const override; is a promise that addBuildingComponent will not change BuildingComponent. However, it tries to modify the member variable building_Org with the push_back()...

Removing the const from addBuildingComponent() should fix the error.

The discards qualifiers part of the error message refers to the conflict with the const qualifier of the member function.

C++ template related error messages can be notoriously difficult to parse at first, but it does get easier with practice :-)

Sign up to request clarification or add additional context in comments.

1 Comment

To OP: the function needs to be made non-const in the base class also.
1

You defined BuildingComponent::addBuildingComponent method as const (i.e. that it won't change member varialbles), but you are adding passed in value to a member list (i.e. changing the member variable).

Comments

0
  1. addBuildingComponent() is a const method. within its scope, *this is const, and so this->building_Org is const.

  2. std::vector::push_back() is a non-const method. So it can't be called in a context where the vector is const.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.