Siddharth
Siddharth

Reputation: 1166

error C2228: left of '.words' must have class/struct/union

I get the following error when I try to compile this file using VS 2013. I've linked the file since it seemed too large to copy and paste here.

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

Code

#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;
}

Upvotes: 1

Views: 1461

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

I think there is a typo. At least here instead of

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

should be

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

It is a very bad practice to use names that differ only in one symbol.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612964

You wrote

spectralFrameWord[0].words

but meant

spectralFrameWords[0].words

The compiler could not have made this much clearer. It said:

left of '.words' must have class/struct/union

So that tells you to inspect that which is to the left of words to see why it does not fit the bill.


When you allocate with new sometype[...] you must deallocate with delete[]. Using std::vector<T> might be more appropriate.

Upvotes: 3

Related Questions