2011-10-26 50 views
1

...是我想要做的。我有以下代碼:根據運行時間創建一個類型的數組

... 
    int len = 0; 
    char c; 
    bool fp = false; 

    while (infile.good()) { 
      c = infile.get(); 
      if (c == '\n') ++len; 
      else if (c == '.') fp = true; 
    } 

    if (fp == true){ 
      float Ai[N]; 
      float *Ao = new float [len]; 
    } else { 
      int Ai[N]; 
      int *Ao = new int [len]; 
    } 

    for (int i=0; i<L; ++i){ 
      for (int j=0; j<N; ++j) infile >> Ai[j]; 
      Ao[i] = findmax(Ai); 
    } 
... 

它應該做的陣列進行雙打,如果該文件中檢測到小數點或者,如果沒有,出整數。

我沒有檢查第一循環還沒有,因爲我沒有得到它來編譯:

warning: unused variable ‘Ai’               
warning: unused variable ‘Ao’ 
warning: unused variable ‘Ai’ 
warning: unused variable ‘Ao’ 
error: ‘Ai’ was not declared in this scope 
error: ‘Ao’ was not declared in this scope 

我想我有如何處理這個任務,不只是一個簡單的錯誤的一個基本問題。

那麼,什麼是錯誤的,以及如何從一開始就修正/使它變得正確?

+0

一旦if塊完成,Ai和Ao就會超出範圍 – slartibartfast

+0

只與您的問題略有相關:像您這樣的輸入循環通常會有錯誤。嘗試'while(infile.get(c)){...}'一次讀取一個字符的輸入。 –

回答

3

編輯: 如上所述,您的編譯器錯誤來自Ao,而Ai被聲明在與您嘗試使用它不同的範圍內。


這是模板真正方便的地方。

template<typename T> 
T *findMaxes(inFile) 
{ 
    T Ai[N]; 
    T *Ao = new T[len]; 

    for (int i = 0; i < L; ++i) 
    { 
     for (int j = 0; j < N; ++j) 
      infile >> Ai[j]; 
     Ao[i] = findmax(Ai); 
    } 

    return Ao; 
} 




int len = 0; 
char c; 
bool fp = false; 

while (infile.good()) { 
     c = infile.get(); 
     if (c == '\n') ++len; 
     else if (c == '.') fp = true; 
} 

if (fp) 
{ 
    float *Ao = findMaxes<float>(inFile); 

    // Do stuff with the floating point array 
} 
else 
{ 
    int *Ao = findMaxes<int>(inFile); 

    // Do stuff with the int array 
} 
0

你說得對,這裏的根本問題。 You Ai變量在關閉「}」時超出範圍。所以你以後不能再使用它們了。這也解釋了警告,因爲你從不在消失之前使用這些變量。

C++是靜態類型的,所以你不能只是在運行時改變變量的類型。

您可以查看工會將float或int放置到變量中。更接近C++的方法可能涉及繼承。但是,如果你只有這兩種情況,你也可以複製代碼(鴨子)。

+0

arg ...至少建議使用類型安全,強大的異常保證boost :: variant <...>而不是普通工會(** [here](http://www.boost.org/doc/libs/1_47_0/doc) /html/variant.html#variant.abstract)** – sehe

+0

同意。這些都是解決問題的更好辦法。 – dantje

1

你的問題是與範圍..如果你在一個if塊中聲明一個變量,它將只存在於該塊內。當你進入最後的循環時,Ao不再存在。你可以試試這個:

if (fp == true) 
{ 
    float Ai[N]; 
    float *Ao = new float [len]; 
    for (int i=0; i<L; ++i) 
    { 
     for (int j=0; j<N; ++j) infile >> Ai[j]; 
     Ao[i] = findmax(Ai); 
    } 
} 
else 
{ 
    int Ai[N]; 
    int *Ao = new int [len]; 
    for (int i=0; i<L; ++i) 
    { 
     for (int j=0; j<N; ++j) infile >> Ai[j]; 
     Ao[i] = findmax(Ai); 
    } 
} 
3

簡短的回答:你不能這樣做。

C++是一種靜態類型語言,這意味着必須在編譯時決定變量的類型(即寫代碼時)。你不能說類似「申報xget_type_from_user()類型。

隨着基於繼承的多態性你可以拼湊一些設置,你通過一個基類的引用處理所有的事情,但實際的實例類型在運行時確定。這幾乎是肯定矯枉過正您的設置,我想,但是這是一個處理上用C細節運行時的依賴性++。然而,這不適用於基本類型,它是類類型的工作的標準方式。

這是一個天真的,刪除類型的過度示例:

class NumberImpl; 

class Number 
{ 
    NumberImpl * pNum; 
public: 
    explicit Number(int n) : pNum(new NumberInteger(n)) { } 
    explicit Number(double d) : pNum(new NumberDouble(d)) { } 
    // .. 
}; 

class NumberImpl { /* ... common implementation interface here ... */ } 

class NumberInteger : public NumberImpl 
{ 
    int n; 
public: 
    NumberInteger(int m) : n(m) { } 
    // ... 
}; 

// and so forth 

這種類型擦除被使用boost.anyshared_ptr,它有一些優點。無論你是否需要它取決於你(但答案是「否」),你可能會快樂地只用一種常見的數字類型。如果你使它成爲long double,你通常會得到64位的整數精度,噸的範圍。

+0

+1我喜歡你是如何深入但很短的 – sehe