c++ - 为什么会出现此错误?

标签 c++ function

<分区>

Possible Duplicate:
error C2248: ‘std::basic_ios<_Elem,_Traits>::basic_ios’ : cannot access private member declared in class ‘std::basic_ios<_Elem,_Traits>’

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

我应该用这段代码做的是打开两个文件,读取文件,使用函数和数组计算文件中数字的均值和标准差。所以,我收到了这个错误,我不确定我的代码有什么问题。我不太确定代码有什么问题,但可能是功能?我的 friend 查看了我的代码并收到了该错误,但之前没有。我应该在某个地方做一个引用,但我把它变成了一个值吗?有人能帮忙吗?抱歉,如果我在发这篇文章时做错了什么。

#include <iostream>
#include <fstream>
#include <cmath>
#include <string> 
#include <iomanip>
using namespace std ;
bool open(ifstream &A_bank, ifstream &B_bank) ;
void read(ifstream &A_bank, ifstream &B_bank, string &n1, string& n2, int &i, int& j,  float &num, float &num1, float &total, float &total1, float counter, float counter1);
void avg(float &mean, float &mean1, float total, float total1, int i, int j);
void print(ifstream A_bank, ifstream B_bank, float mean, float mean1, string n1, string n2);
int main()
 {
    //Declaring variables.
    ifstream A_bank, B_bank ;
    string n1,n2;
    int i, j, a[20], b[20] ;
    float num=0, num1=0, total=0, total1=0, stdev=0,stdev1=0, mean=0, mean1=0, counter, counter1 ;

    open(A_bank, B_bank) ;
    read(A_bank, B_bank, n1, n2, i, j, num, num1, total, total1, counter, counter1) ;
    avg(mean, mean1, total, total1, i, j) ;
    print(A_bank, B_bank, mean, mean1, n1, n2) ;
    return 0;
 }

bool open(ifstream &A_bank, ifstream &B_bank)
{

    string n1, n2;
    cout << "Enter file name: " ;
    getline(cin, n1) ;
    A_bank.open(n1.c_str()) ; 
    if (A_bank.eof())
    {
        cout << "File is empty" << endl ;
        return false ;
    }

    //Verify that the correct file name was entered.
    else if (A_bank.fail())
    {
        cout << "File could not be opened." << endl ;
        return false ;
    }

    cout << "Enter file name of second bank: " ;
    getline(cin, n2) ;
    B_bank.open(n2.c_str()) ;
    if (B_bank.eof())
    {
        cout << "File is empty" << endl ;
        return false ;
    }
    else if (B_bank.fail())
    {
        cout << "File could not be opened." << endl ;
        return false ;
    }
    return true ;
}



//Reading the files
void read(ifstream &A_bank, ifstream &B_bank, string &n1, string& n2, int &i, int& j, float &num, float &num1, float &total, float &total1, float counter, float counter1, int a[], int b[])
{
    getline(A_bank,n1);
    for(int i=0; !A_bank.eof();i++)
    {
        A_bank>>a[i];
        total+=a[i];
        counter++;
    }
    getline(B_bank,n2);
    for(int j=0; !B_bank.eof();j++)
    {
        B_bank>>b[j];
        total+=b[j];
        counter1++;
    }
}

//Calculations
void avg(float &mean, float &mean1, float total, float total1, int i, int j)
{
    mean = (total) / (i) ;
    mean1 = (total1) / (j) ;
}

最佳答案

print() 中,您试图复制一个 ifstream 对象,这是不可能的。使用 ifstream& 代替:

void print(ifstream &A_bank, ifstream &B_bank, float mean, float mean1, string n1, string n2);

根据 std::basic_ifstream::basic_ifstream使用 C++11 可以移动。

关于c++ - 为什么会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13365877/

相关文章:

c++ - 在 C++ 中使用指向对象的指针和 operator new 来定义对象

c++ - 如何优化 std::map insert() 函数?

c++ - 为提升线程命名?

C++将带参数的函数指针传递给函数

javascript - React - 使用 Map 渲染对象数组

c++ - 在 C++ 代码中计算斐波那契数列

c++ - 函数指针问题

C++ 获取对象

r - 如何运行从 eventReactive 内部获取数据并在表中绘制的函数?

脚本中的 JavaScript 匿名函数