2011-07-12 44 views
5

我無法理解此錯誤。不在我正在調試的類中。 (是嗎?) 的錯誤是:錯誤C2248:'std :: basic_ios <_Elem,_Traits> :: basic_ios':無法訪問在類中聲明的私有成員'std :: basic_ios <_Elem,_Traits>'

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 ========== 

可能是什麼我收到此錯誤的原因?代碼太長,無法發佈。 ,我使用的編譯器是微軟的Visual C++ 2010速成

編輯:

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

-1希望我們能夠介意閱讀代碼。一旦最小化測試用例或至少發佈錯誤點+其他相關部分,就會刪除-1。 –

+0

@ phresnel我已編輯帖子 – saplingPro

回答

13

您正試圖複製流,這是不可能的。

爲了幫助您避免這種情況,其中一個基類具有私有拷貝構造函數。

+0

你可否詳細說一下。請使用代碼 – saplingPro

+0

出於某種原因,我現在無法編輯答案,因此我添加了有問題的行的新答案。 –

+0

謝謝!錯誤解決了。但是我打開了一個不同的流。儘管路徑相同,我還沒有複製那個流? ! – saplingPro

0

從你的錯誤中,我可以猜測你試圖將operator <<operator >>fstream(或ostream)重載爲你自己的class對象。

不知怎的,你已經錯過一提的是operator <<功能朋友class的。你的代碼應該是like this snippet。仔細查看數據成員爲目標class製作爲public

3

違規的線條,

ifstream readRoom(checkStudentFST); 

,並

ifstream readRoom(checkStudentSND); 

,你要複製的,而不是或許與文件名打開它現有的流。

0

如果要設置一個類參數的構造函數(這是使對象的副本),這樣可能會出現問題:

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

如果FileHandler類包含一個成員是一個文件流,你會得到錯誤,就像我一樣。

SOLUTION:將構造函數參數從FileHandler fh_更改爲FileHandler* fh_

相關問題