错误 C2248:“std::basic_ios<_Elem,_Traits>::basic_ios”:无法访问在类“std::basic_ios<_Elem,_Traits>

Posted

技术标签:

【中文标题】错误 C2248:“std::basic_ios<_Elem,_Traits>::basic_ios”:无法访问在类“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>' 【发布时间】:2011-07-12 08:14:55 【问题描述】:

我无法理解这个错误。这个错误不在我正在调试的类中。 (是吗?) 错误是:

c:\program files\microsoft visual studio 10.0\vc\include\fstream(890): error    C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ios(176) : see     declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated   function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我收到此错误的原因可能是什么?代码太长,无法发布。 我使用的编译器是 Microsoft visual c++ 2010 express

编辑:

    void HostelManager::guestMonitor() 
        system("cls");

        cout << "Name of the guest : "; // cin guest
        cin.ignore();
        getline( cin , guestName );

        cout << "\nWhom to meet : ";   // to whom the guest wants to meet
        cin.ignore();
        getline( cin , fullName ); 

        string pathGND = "d:/HostelManager/studentDetails/groundFloor/" + fullName + ".txt";
        string pathFST = "d:/HostelManager/studentDetails/firstFloor/" + fullName + ".txt";
        string pathSND = "d:/HostelManager/studentDetails/firstFloor/" + fullName + ".txt";
        ifstream checkStudentGND( pathGND );
        ifstream checkStudentFST( pathFST );
        ifstream checkStudentSND( pathSND );
        string x = "Floor";

        if( checkStudentGND ) 
            ifstream readRoom( pathGND );
            readRoom >> fullName >> fatherName >> permanentAddress >> postalAddress >> x >> roomNumberToBeAlloted_GND;
            int hisRoomNumber = roomNumberToBeAlloted_GND;
            readRoom.close();
            storeGuestData( hisRoomNumber , fullName );  // call to store guest data
         else if( checkStudentFST ) 
            ifstream readRoom( checkStudentFST );
            readRoom >> fullName >> fatherName >> permanentAddress >> postalAddress >> x >> roomNumberToBeAlloted_FST;
            int hisRoomNumber = roomNumberToBeAlloted_FST;
            storeGuestData( hisRoomNumber , fullName );  // call to store guest data
         else if( checkStudentSND ) 
            ifstream readRoom( checkStudentSND );
            readRoom >> fullName >> fatherName >> permanentAddress >> postalAddress >> x >> roomNumberToBeAlloted_SND;
            int hisRoomNumber = roomNumberToBeAlloted_SND;
            storeGuestData( hisRoomNumber , fullName );  // call to store guest data
         else 
            string sorryMSG = "Sorry ! The student is not in this hostel"                  cout << sorryMSG << endl ;
        
   

被调用函数:

    void HostelManager::storeGuestData( int hisRoomNumber , string hisName ) 
        cout << "\nVisitor's Address : ";  // cin visitor address
        string visitorAddress;
        getline( cin , visitorAddress );

        cout << "\nRelation with the student : ";   // cin relation with student
        string relation;
        getline( cin , relation );

        cout << "\nEnter Month";    // cin enter month
        short month;
        cin >> month;

        cout << "\nEnter Date";    // cin enter date
        short date;
        cin >> date;

        cout << "\nYou can meet " << hisName << " in room number " << hisRoomNumber << endl ;

        string visitorPath = "d:/HostelManager/GuestMonitor/" + fullName + ".txt" ;
        ofstream guestEntry( visitorPath );
        guestEntry << "Name of Visitor : " << guestName << endl ;
        guestEntry << "To meet : " << fullName << endl ;
        guestEntry << "Address of visitor : " << visitorAddress << endl ;
        guestEntry << "Relation with the student : " << relation << endl ;
        guestEntry << "Month : " << month << endl ;
        guestEntry << "Date : " << date << endl ; 
        guestEntry.close();
        cout << "\n\n\n\nData successfully inserted";
    

数据成员:

    private:
        string fullName;
        string fatherName;
        string permanentAddress;
        string postalAddress;
        int mobileNumbers[2][10];
        double percentageMarks;
        int roomNumber;
        int roomNumberToBeAlloted_GND ;
        int roomNumberToBeAlloted_FST ;
        int roomNumberToBeAlloted_SND ;
        string preferredFloor;
        string guestName;
        string studentFined;
        string complaintAgainst;
        string receiverOfEmail;
        string senderOfEmail;
        string subject;

    public:
        void guestMonitor();
        void storeGuestData( int hisRoomNumber , string hisName );

【问题讨论】:

您能否至少发布发生此错误的代码部分? -1 期望我们用心读代码。一旦发布了最小测试用例或至少错误点+其他相关部分,将删除 -1。 @phresnel 我已经编辑了帖子 @grassPro:您必须省略 @ 和名称之间的空格才能通知我。除此之外:您的代码格式不正确并且比需要的难以阅读。也许使用您的编辑器自动缩进。无论如何,我会取消我的反对票。 【参考方案1】:

您正在尝试复制流,这是不可能的。

为了帮助您避免这种情况,其中一个基类有一个私有复制构造函数。

【讨论】:

由于某种原因,我现在无法编辑答案,所以我添加了一个带有问题行的新答案。 谢谢!错误已解决。但我打开了一个不同的流。虽然路径相同,但我没有复制那个流? ! 您已经打开同一个文件两次(用于阅读)。这可能有效,也可能无效,具体取决于您的操作系统和文件访问的默认设置。 有人可以提示如何找到导致此错误的代码吗?我有一个相当大的代码库并且遇到了这个错误(仅针对 basic_ofstream),但是这个错误消息只是指向框架文件而不是实际的代码文件。 @lowglider - 开始一个新问题并显示(部分)您的代码和您收到的确切错误消息可能会更好。【参考方案2】:

有问题的行是,

ifstream readRoom( checkStudentFST ); 

和,

ifstream readRoom( checkStudentSND );  

您尝试复制现有流的位置,而不是使用文件名打开它。

【讨论】:

【参考方案3】:

根据您的错误,我可以猜测您正在尝试使用 fstream(或 ostream)为您自己的 class 对象重载 operator &lt;&lt;operator &gt;&gt;

不知何故,您忘记提及operator &lt;&lt; 是您class朋友。你的代码应该是like this snippet。仔细查看数据成员是为目标class 制作的public

【讨论】:

【参考方案4】:

如果您在构造函数中设置类参数(正在制作对象的副本),则可能会出现问题,如下所示:

class Test

public:
    Test() 
    Test(FileHandler fh_) : fh(fh_) 
    // ...
private:
    FileHandler fh;
;

如果FileHandler 类包含一个文件流成员,你会得到错误,就像我一样。

解决方案:将构造函数参数从 FileHandler fh_ 更改为 FileHandler* fh_

【讨论】:

以上是关于错误 C2248:“std::basic_ios<_Elem,_Traits>::basic_ios”:无法访问在类“std::basic_ios<_Elem,_Traits>的主要内容,如果未能解决你的问题,请参考以下文章

QT-MSVC 错误 C2248

c++中如何用函数传递fstream类型

错误:C2248:“QVariant::QVariant”:无法访问在“QVariant”类中声明的私有成员

命名空间内的错误 c2248 朋友类

“错误C2248:'CObject :: CObject':无法访问在类'CObject'中声明的私有成员[重复]

错误 C2248:“klientPracownik::klientPracownik”:无法访问在“klientPracownik”类中声明的私有成员