【问题标题】:Error with binary storage tree fstream二进制存储树 fstream 出错
【发布时间】:2012-05-07 12:35:23
【问题描述】:

我目前有一个项目,我通过一个包含数千个名称的文本文件运行并将它们放入二进制存储树中。遇到此错误的绊脚石:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream  1116

我希望有人可以帮助我解释根本问题。

提前致谢。

乔希

编辑:

BinaryTreeStorage::BinaryTreeStorage(void) : root(NULL)
{

}

BinaryTreeStorage::~BinaryTreeStorage(void)
{

}

void BinaryTreeStorage::insert(string &input, TreeNode *&root)
{
if(root != NULL)
{
    root -> name = input;
    root -> left = NULL;
    root -> right = NULL;
}

else if (input < root -> name)
{
    insert(input, root -> left);
}
else
{
    insert(input, root -> left);
}   
}

string BinaryTreeStorage:: writeToTree(TreeNode *&root)
{
if(root ->left != NULL)
{
    writeToTree(root ->left);
}
return root->name;
if (root->right != NULL)
{
    writeToTree(root);
}
}

void BinaryTreeStorage::write(ofstream nameOut)
{
cout << "Writing out bst names" << endl;
writeToTree(root);
}

void BinaryTreeStorage::read(ifstream& nameIn)
{
cout<< "Reading in bst" << endl;
string name;

for (int i = 0; i < numberOfNames; i++)
{
    nameIn >> name;
    insert (name, root);
}
}

【问题讨论】:

    标签: c++ error-handling tree storage binary-tree


    【解决方案1】:

    在您的write 函数中:您不能复制ofstream。通过引用传递它。再说一次,你似乎从来没有在正文中使用 nameOut 函数参数,所以为什么不完全省略它:

    void BinaryTreeStorage::write()
    {
        cout << "Writing out bst names" << endl;
        writeToTree(root);
    }
    

    【讨论】:

    • 啊,是的,谢谢。唯一的问题是我现在的主要问题,因为调用需要一个参数。 binaryTreeStorage1.write(out8);
    • @Josh:好吧,也把它从那里删除!或者将std::ofstream&amp; 传递给函数。
    • 这是一个赋值,很遗憾我们不能更改主方法类。我想我现在知道该怎么做了,谢谢你的帮助。
    • 啊,又一个卑鄙的家庭作业问题。如果是,将您的问题标记为“作业”;网站常见问题解答中对此进行了充分解释。
    • 啊,对不起。我会在未来。我意识到我自己做这件事很重要,这就是为什么我没有对它提出太多要求,而是提出了一个一般性的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2015-05-19
    • 2022-07-03
    • 2020-11-06
    相关资源
    最近更新 更多