如果你在C++,只是做:
struct Coder
{
float **data;
};
typedef Coder *AUTO;
而且你必須確保AUTO
聲明您struct
或聲明的後面做,你可以前瞻性聲明您struct
。
此外,有可能NULL
是未定義的。
您可以用0
替換它,或者只是看看我剛剛給你的鏈接。
這是live example。
編輯: 你給了我們不能工作的代碼:
#include <tchar.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <iostream>
struct Coder {
float **data; // 2-D matrix
};
typedef Coder *AUTO;
AUTO myFeatures = new Coder;
myFeatures->data = NULL; // Forbidden
int main(){
myFeatures->data = new float *[2];
myFeatures->data[0] = new float [2];
}
只能在命名空間範圍的聲明,不表達這樣的。
在標準的§7.3.1/ 1:
名稱空間結構體:
聲明-SEQ 選擇
它說的命名空間人可以可選只包含聲明。
此代碼將工作:
// Your includes
struct Coder {
Coder() : data(NULL) {} // Constructor who initialize data to NULL
// via the member-initialization-list
float **data; // 2-D matrix
};
typedef Coder *AUTO;
AUTO myFeatures = new Coder; // Do you want to keep it in global ??
// It's not a really good design
int main(){
// You should put your declaration here.
// myFeatures->data = NULL; useless now
myFeatures->data = new float *[2];
myFeatures->data[0] = new float [2];
}
你能發表錯誤代碼嗎? – billz
@billz:「這個聲明沒有存儲類或類型說明符」 – Bryan
我不能在我的編譯器上重複這個問題。你能發佈更多的代碼嗎?至少代碼圍繞'myInstance-> data = NULL;' – billz