2016-03-05 31 views
1

我有一個相當簡單的程序。它從看起來像這樣的文件中讀取矩陣從文件中讀取矩陣,給它主

4 4 

a b c d 
a b c d 
a b c d 
a b c d 

並且在控制檯中輸出它。該計劃如預期運行:

#include <iostream> 
#include <fstream> 

using namespace std; 

int main() { 

    int n, m; 
    ifstream myfile; 
    myfile.open ("matrix.txt"); 
    myfile >> n >> m; 
    char mat[n][m]; 

    for (int i = 0; i < n; i++) { 
    for (int j = 0; j < m; j++) { 
     myfile >> mat[i][j]; 
    } 
    } 
    for (int i = 0; i < n; i++) { 
    for (int j = 0; j < m; j++) { 
     cout << mat[i][j]; 
    } 
    cout << endl; 
    } 
    return 0; 
} 

現在,這個計劃是要長出很多,所以我想開始使用headerfiles和聲明的功能外,使我的主要看上去更像

int main() { 
    readMat(); 
    printMat(); 
} 

我的問題是,我真的不知道如何做到這一點。我應該在哪裏聲明矩陣,以便兩個函數都能「看到」它?我不能在全球範圍內聲明它爲char mat[n][m],因爲我只知道readMat()內部的nm

或者是我的整個設計有缺陷,有更好的方法來做到這一點? 我會感激每一個小小的提示。

我還沒有在C++中使用多個文件。

回答

1

不能使用這樣的數組,而不僅僅是多個翻譯單元,但涉及多個功能。

任何函數都需要知道它接收的確切數據類型作爲參數,並且這必須在編譯時聲明。

在這裏,直到運行時才知道數組的類型。 array[10]array[15]是不同的類型。不要被他們的名字可能相同的事實所愚弄。這不是重要的名稱,而是類型,如int [10]int [11]

你有兩個基本的選擇:

  • 使用模板。

  • 將您的陣列轉換爲std::vector<std::vector<int>>。然後,您可以全局聲明該數組(現在是一個向量),並/或將其作爲參數傳遞給您的多個函數。

+0

使用'矢量<矢量>'工作謝謝:) –

1

Matrix.h

#ifndef MATRIX_H 
#define MATRIX_H 

namespace mynamespace { 

template <class T> 
class Matrix { 
private: 
    int m_rows; 
    int m_columns; 

    T** m_data; 

public:  
    Matrix(); // Default Constructor - Null or Empty Matrix 
    Matrix(const int rows, const int columns); // Empty Matrix With Defined Size 
    Matrix(const int rows, const int columns, const T** data); // Defined Matrix 

    T** getData() const; 

}; // Matrix 

#include "Matrix.inl" 

} // mynamespace 

#endif // MATRIX_H 

Matrix.inl

// Matrix Constructor and Function Implementations Here 

Matrix.cpp

#include "Matrix.h" 

Main.cpp的

#include <string> 
#include <iostream> 
#include <fstream> 
#include "Matrix.h" 

using namespace mynamespace; 

void readMatrix(std::fstream& fin, const std::string& strFilename, Matrix& matrixIn); 
void printMatrix(const Matrix& matrix); 

int main() {   
    std::fstream fileIn; // Prefer to not use "using namespace std;" 
    std::string strFilename("Matrix.txt"); 

    Matrix<type> mat(); // Default Empty Matrix To Be Filled In 

    readMatrix(fileIn, strFilename, mat); 
    printMatrix(mat); 

    return 0; 

} // main 

void readMatrix(std::fstream& fin, const std::string& strFilename, Matrix& mat) { 
    // Do Work Here To Populate Matrix Object 
} // readMatrix 

void printMatrix(Matrix& mat) { 
    // Do Work Here To Print Matrix Being Passed In 
} // printMatrix 

利用該僞代碼中,設計在這裏允許構造的任何類型的基質。矩陣是模板類型的對象。通過從文件中讀取數據來生成矩陣的實現以及顯示或打印矩陣的實現與類本身無關。這允許矩陣類儘可能通用,而不必依賴任何其他庫。做這項工作的函數是接受矩陣對象的獨立函數。在您的文本或二進制文件中可能需要修改以使用此構造。

matrix.txt

type 
4 4 

a b c d 
a b c d 
a b c d 
a b c d 

唯一的問題與此,是現在應該已經是顯而易見的:你需要定義實例化時,這個矩陣將持有什麼類型的數據。然而,直到讀入文件之後,您才知道該類型。因此,此代碼確實存在需要解決的問題。但是,這裏使用的總體設計方法僅僅是展示如何將類似的代碼對象包含在它們自己的類對象中,同時將獨立的代碼塊分離開來,這些代碼塊對它們起作用。現在其他類對象可能具有文件讀取和打印方法,但通常是Vector(數學公式&而非容器),而Matrix類通常不會。

+0

謝謝你的回答,這是非常啓發。雖然我沒有使用它(只是使用'std :: vector >'就足夠了這種情況),我將它保存爲模板供以後使用。 –