为什么我收到此错误?

时间:2012-11-13 17:44:22

标签: c++ function

  

可能重复:
  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>'

我应该对此代码执行的操作是打开两个文件,读取文件,使用函数和数组计算该文件内部数字的平均值和标准差。所以,我收到这个错误,我不确定我的代码有什么问题。我不确定代码有什么问题但它可能是函数吗?我的朋友看了我的代码并得到了这个错误,但之前没有。我应该在某个地方做一个参考,但我把它作为一个值而不是?有人可以帮忙吗?对不起,如果我在发帖时做错了什么。

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

1 个答案:

答案 0 :(得分:6)

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进行移动。