2013-12-15 40 views
1

當我嘗試使用VS 2013編譯此文件時,出現以下錯誤:我已鏈接文件,因爲它似乎太大而無法複製和粘貼這裏。錯誤C2228:'.words'的左邊必須有class/struct/union

 
Payload.cc(77) : error C2275: 'spectralFrameWord' : illegal use of this type as an expression 
Payload.cc(22) : see declaration of 'spectralFrameWord' 
Payload.cc(77) : error C2228: left of '.words' must have class/struct/union 

代碼

#include <cstdio> 
#include <cstdint> 
using namespace std; 

long getFileSize(FILE *file) { 
    long currentPosition, endPosition; 
    currentPosition = ftell(file); 
    fseek(file, 0, 2); 
    endPosition = ftell(file); 
    fseek(file, currentPosition, 0); 
    return endPosition; 
} 

struct frame { 
    uint8_t bytes[535]; 
}; 

struct spectralFrame { 
    uint8_t bytes[512]; 
}; 

struct spectralFrameWord { 
    uint16_t words[256]; 
}; 

int main() { 
    char *filePath = "C:\\Payload\\Untitled.bin"; 
    uint8_t *fileBuffer; 
    FILE *file = NULL; 
    file = fopen(filePath, "rb"); 
/* if((file = fopen(filePath, "rb")) == NULL) 
     cout << "Failure." << endl; 
    else 
     cout << "Success." << endl; 
*/ 
    long fileSize = getFileSize(file); 
    fileBuffer = new uint8_t[fileSize]; 
    fread(fileBuffer, fileSize, 1, file); 
    fclose (file); 
    long frameCount = fileSize/535; 
    frame *frames = new frame[frameCount]; 
    for(int i = 0, j = 0, k = 0; i < fileSize; i++) { 
     frames[j].bytes[k++] = fileBuffer[i]; 
     if((i % 534) == 0) { 
      j++; 
      k = 0; 
     } 
    } 
    delete fileBuffer; 
    fileBuffer = NULL; 
    spectralFrame *spectralFrames = new spectralFrame[frameCount]; 
    for(int i = 0; i < frameCount; i++) { 
     for(int j = 22, k = 0; j < 534; j++) { 
      spectralFrames[i].bytes[k++] = frames[i].bytes[j]; 
     } 
    } 
    delete frames; 
    frames = NULL; 
    spectralFrameWord *spectralFrameWords = new spectralFrameWord[frameCount]; 
    uint16_t word; 
    for(int i = 0; i < frameCount; i++) { 
     for(int j = 0, k = 0; i < 511;) { 
      word = spectralFrames[i].bytes[j++] << 8; 
      spectralFrameWords[i].words[k++] = word | spectralFrames[i].bytes[j++]; 
     } 
    } 
    delete spectralFrames; 
    spectralFrames = NULL; 
    filePath = "C:\\Payload\\Untitled.tsv"; 
    file = fopen(filePath, "w"); 
/* if((file = fopen(filePath, "w")) == NULL) 
     cout << "Failure." << endl; 
    else 
     cout << "Success." << endl; 
*/ 
    for(int i = 0; i < 256; i++) { 
     fprintf (file, "%u\t%u\n", i, spectralFrameWord[0].words[i]); 
    } 
    fclose (file); 
    return 0; 
} 
+0

顯示錯誤中引用的行(77和22,以及上下文的周圍行)。 –

回答

3

你寫

spectralFrameWord[0].words 

但意味着

spectralFrameWords[0].words 

編譯器不能把這個更清楚。它說:

「.words'的左邊必須有類/結構/聯合

所以,告訴你檢查該是到words左邊,看看它爲什麼不適合法案。


當你與new sometype[...]分配必須與delete[]解除分配。使用std::vector<T>可能更合適。

+0

感嘆。那是一個愚蠢的錯誤。謝謝。 – Siddharth

1

我認爲有一個錯字。至少不是

for(int i = 0; i < 256; i++) { 
    fprintf (file, "%u\t%u\n", i, spectralFrameWord[0].words[i]); 
} 

這裏應該是

for(int i = 0; i < 256; i++) { 
    fprintf (file, "%u\t%u\n", i, spectralFrameWords[0].words[i]); 
} 

這是一個非常不好的做法,只能使用一個符號,它的名字不同。

+0

你說得對。我需要制定這些標識符。 – Siddharth

相關問題