简体   繁体   中英

I am getting a 'base(const std::ios_base)' is private error in my code. What is wrong with it?

I am getting an error and it happens right at line Chest_vect.push_back(newChest); From my knowledge I don't see anything wrong with it. But I am sure I probably missed something small. If you can help that would be appreciated! Thanks either way!

class Chest_objects {

private:
int loc_x, loc_y;
int map_x, map_y;
string obj_name = "";
string obj_id = "";
bool obj_trigger = false;
bool obj_visible = false;

public:

void setLoc_x(int x)
{
 loc_x = x;
}
void setLoc_y(int y)
{
 loc_y = y;
}
                              //OTHER GETTER AND SETTER FUNCTIONS NOT INCLUDED

Chest_objects(int x, int y);  //CONSTRUCTOR FUNCTIONS: NOTE I DON'T HAVE A COMPLETE ONE
Chest_objects();
Chest_objects(int x, int y, int my, int mx, string name, string id, bool trig, bool vis);

int getChestVect(vector<Chest_objects>& Chest_vect, const char*getFile)
{
int amount = 0;
int get_x = 0;
int get_y = 0;
int max_x;
int max_y;
char get_info;
ifstream file;
file.open(getFile);
file >> max_x >> max_y;

while(get_info != '0')
    {
        file >> get_info;
        if(get_info == '.')
        {
        get_x++;
        }
        if(get_info == 'B')
        {
        Chest_objects newChest(get_x, get_y);
        Chest_vect.push_back(newChest);
        get_x++;
        amount++;
        }
        if(get_x >= max_x)
        {
        get_x = 0;
        get_y++;
        }

    }
return amount;
}
};

int main()
{
... blah
return 0;
}

Chest_objects::Chest_objects(int x, int y)
{
loc_x = x;
loc_y = y;
}

 Chest_objects::Chest_objects()
{
loc_x = 0;
loc_y = 0;
}

error: In instantiation of 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = Chest_objects; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename std::_Vector_base<_Tp, _Alloc>::pointer = Chest_objects*]':|

error: 'std::ios_base::ios_base(const std::ios_base&)' is private|

The push_back method requires that the type contained in the container be copyable. The error implies that the Chest_objects class has only a private constructor. So check that Chest_objects has:

  • A copy constructor: Chest_objects(const Chest_objects&);
  • A copy operator: Chest_objects& operator=(const Chest_objects& rhs);

and that they are both public .

Another possibility is that you subclasses Chest_objects from one of the classes in <iostream> , which is not recommended, and the compiler's seeing a constructor that takes std::ios marked private , which means you can't instantiate or subclass it. This can happen with singletons.

But there's no way to be sure since you didn't post all the code. Also, before posting code, try to cut out as much code as possible while preserving the error.


There's your problem:

ifstream small_obj_map;

You're instantiating an ifstream inside the object. Every object in the Chest_vect will have its own ifstream . This is almost certainly not what you want.

Since you have not written copy constructors or copy operators, the compiler is trying to create them for you. And the default copy constructor will try to construct small_obj_map from the small_obj_map of the object being copied. But ifstream has no copy constructor because it's not meant to be copied.

You should instead create the ifstream only when you need it, inside the method that needs it, and let it be destroyed when the method exits. It should not be a part of the object because it has no need to persist with the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM