2013-12-08 26 views
0
//************************************************************************************************************** 
// FILE: a04.cpp 
//************************************************************************************************************** 
#include <fstream> 
#include <iostream> 
#include "Sorter.hpp" 
using namespace std; 
// Write the function header for Merge() 
void Merge(int a[] , int b[],int c[], int n) 
{ 
    int i = 0, j = 0, k = 0; 
    while (i < n || j < n) { 
     // Write the if-elseif-elseif-... statement in the body of the while loop 
     if (i < n && j < n && a<=b) { 
       c = a; 
       i++; 
     } 
     else if (i < n && j < n && a > b) { 
      c =b; 
      j++; 
     } 
     else if (i < n) { 
      c = a; 
      i++; 
     } 
     else { 
      c =b; 
      j++; 
     } 
     k++; 
    } // end while 
} // end function merge 

void ReadList(ifstream fin, int a[], int n); 

void ReadFile(int a[], int b[], int& n) 
{ 
    ifstream fin ("ints-in.txt"); 
    ReadList(fin,a,n); // come back again different than the psuedo code 
    ReadList(fin,b,n); 
    fin.close(); 
} 


// Write the function header for ReadList() 
void ReadList(ifstream fin, int a[], int n) 
{ 
    n = 0; 
    int number; 
    // Implement the sentinel loop 
    while (fin >> number && number != -1) { 
     a[n] = number; 
     n++; // come back again 
    } 
} 


void WriteFile(int a[], int n) 
{ 
    ofstream fout("ints-out.txt"); 
    // Write a vary loop where variable ivaries from 0 to n - 1. In the loop send a[i] and ' ' to fout. 
    for (int i =0; i <= n-1; i++) { 
     fout << a << ' '; // come back again. 
    } 
    fout << -1; 
    fout.close(); 
} 


int main() 
{ 
// Define int arrays a,b, and c and int variable n. 
int a[100], b[100], c[200]; 
int n; 

// Call ReadFile() passing a,b, and n 
ReadFile(a,b,n); 

// Define and create a Sorter object named sorter 
Sorter sorter; 
// Call SelectionSort() on sorter to sort a into ascending order. 
// Hint: to access the ASCENDING constant data member of the Sorter class you write Sorter::ASCENDING. 
sorter.SelectionSort(a,100,Sorter::ASCENDING); //come back again here. 

// Call BubbleSort() on sorter to sort b into ascending order 
sorter.BubbleSort(b,100,Sorter::ASCENDING); 
// Call Merge() passing a,b,c, and n 
Merge(a,b,c,n); 
// Call WriteFile() passing c and 2n 
WriteFile(c,2*n); 

return 0; 
} 

我不知道爲什麼它不能被編譯。一個C++代碼不能被編譯,因爲ifstream是不可訪問的

錯誤1錯誤C2248: '的std :: basic_ifstream < _Elem,_Traits> :: basic_ifstream':不能訪問私人 構件 類聲明爲 '標準:: basic_ifstream < _Elem,_Traits>' C:\ users \ hisham \ documents \ visual studio 2012 \ projects \ homework4 \ a04.cpp 39 1家庭作業4錯誤2錯誤 C2248:'std :: basic_ifstream < _Elem,_Traits> :: basic_ifstream':不能 訪問在類中聲明的私有成員 'std :: basic_ifstream < _Elem,_Traits>'c:\ users \ hisham \ documents \ visual studio 2012 \ projects \ homework4 \ a04.cpp 40 1 Homework4 3智能感知: 「的std :: basic_ifstream < _Elem,_Traits> :: basic_ifstream(常量 的std :: basic_ifstream < _Elem,_Traits> :: _ MYT & _right)[與_Elem =炭, (C:\ Program Files(x86)\ Microsoft Visual Studio 11.0 \ VC \ include \ fstream「)的第827行中聲明的」_Traits = std :: char_traits「)是 無法訪問的c:\ Users \ Hisham \ Documents \ Visual Studio 2012 \項目\ Homework4 \ a04.cpp 39 11 Homework4 4智能感知: 「的std :: basic_ifstream < _Elem,_Traits> :: basic_ifstream(常量 的std :: basic_ifstream < _Elem,_Traits> :: _ MYT & _right)[與_Elem = char, _Traits = std :: char_traits] 「(在」C:\ Program Files文件(x86)\ Microsoft Visual Studio 11.0 \ VC \ include \ fstream「的第827行中聲明)爲 無法訪問c:\ Users \ Hisham \ Documents \ Visual Studio 2012 \ Projects \ Homework4 \ a04.cpp 40 11作業4

謝謝。

回答

5

錯誤未說明ifstream無法訪問。它說(以自己錯綜複雜的方式)複製構造函數是不可訪問的,因爲它被聲明爲私有的。問題是你正在通過值ifstream,它試圖調用不可訪問的複製構造函數。

void ReadList(ifstream fin, int a[], int n) 

而是通過引用傳遞。

void ReadList(ifstream & fin, int a[], int n) 
1

iostreams不可複製,所以不能通過值來傳遞它們。

1
void ReadFile(int a[], int b[], int& n) 
{ 
    ifstream fin ("ints-in.txt"); 
    ReadList(fin,a,n); // come back again different than the psuedo code 
    ReadList(fin,b,n); 
    fin.close(); 
} 

void ReadList(ifstream fin, int a[], int n) 

您試圖通過複製傳遞istream實例。你不能那樣做 - 拷貝構造函數被標記爲私有的。

您必須聲明你ReadList功能:

void ReadList(ifstream& fin, int a[], int n) 
0

我一直工作在相同的代碼,我有東西在合併()函數不同。

void Merge(int a[], int b[], int c[], int& n) 
{ 
    int i = 0, j = 0, k = 0; 
    while (i < n || j < n) 
{ 
    if (i < n && j < n && a[i] <= b[j]) 
    {c[k] = a[i]; 
    i++;} 
    else if (i < n && j < n && a[i] > b[j]) 
    {c[k] = b[j]; 
    j++;} 
    else if (i < n) 
    {c[k] = a[i]; 
    i++;} 
    else 
    {c[k] = b[j]; 
    j++;} 
    k++; 
     } 
    } 

我把i,j和k放在括號中。 我不確定這是否正確,但我沒有任何錯誤信息。