【问题标题】:I am getting a 'base(const std::ios_base)' is private error in my code. What is wrong with it?我的代码中出现“base(const std::ios_base)”是私有错误。它有什么问题?
【发布时间】:2014-01-18 01:58:06
【问题描述】:

我收到一个错误,它发生在 Chest_vect.push_back(newChest); 行据我所知,我认为它没有任何问题。但我确信我可能错过了一些小事。如果您能提供帮助,将不胜感激!不管怎样,谢谢!

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;
}

错误:在 '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 >;类型名 std::_Vector_base<_tp _alloc>::pointer = Chest_objects*]':|

错误:'std::ios_base::ios_base(const std::ios_base&)' 是私有的|

【问题讨论】:

    标签: c++ vector ifstream


    【解决方案1】:

    push_back 方法要求容器中包含的类型是可复制的。该错误意味着Chest_objects 类只有一个私有构造函数。所以检查Chest_objects 有:

    • 复制构造函数:Chest_objects(const Chest_objects&amp;);
    • 复制操作员:Chest_objects&amp; operator=(const Chest_objects&amp; rhs);

    他们都是public

    另一种可能性是您从&lt;iostream&gt; 中的一个类中继承Chest_objects,这是不推荐的,并且编译器会看到一个使用std::ios 标记为private 的构造函数,这意味着您无法实例化或子类化它。这可能发生在单例中。

    但是由于您没有发布所有代码,因此无法确定。此外,在发布代码之前,请尝试在保留错误的同时尽可能多地删除代码。


    这是你的问题:

    ifstream small_obj_map;
    

    您正在对象内部实例化 ifstreamChest_vect 中的每个对象都有自己的ifstream。这几乎肯定不是你想要的。

    由于您尚未编写复制构造函数或复制运算符,编译器正在尝试为您创建它们。而默认的复制构造函数将尝试从被复制对象的small_obj_map 构造small_obj_map。但是ifstreamhas no copy constructor 因为它不是用来复制的。

    您应该只在需要时在需要它的方法内创建ifstream,并在方法退出时将其销毁。它不应该是对象的一部分,因为它不需要与对象保持一致。

    【讨论】:

    • 感谢您的回复。是的,我认为您对构造函数是正确的,但不确定。我应该只发布课程和功能吗?
    • 发布类的声明行,包括任何基类和构造函数声明,以及任何operator= or 和类型转换运算符声明。并发布 exact 错误消息,逐个字符。
    • 我尽量保持简单,没有运算符和几个构造函数。我声明了一个覆盖构造函数但没有定义它。不知道是不是这样。此文件中没有类继承。
    • 不是这样的。是 small_obj_map 让你崩溃。请参阅上面的更新答案。
    • 所以我应该在 main 中使用 ifstream 而不是在类中似乎是私有的多个副本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多