【问题标题】:error C2228: left of '.words' must have class/struct/union错误 C2228:“.words”的左侧必须有类/结构/联合
【发布时间】:2013-12-16 03:06:15
【问题描述】:

当我尝试使用 VS 2013 编译此文件时出现以下错误。我已链接该文件,因为它似乎太大而无法在此处复制和粘贴。

Payload.cc(77) : error C2275: 'spectralFrameWord' : 非法使用这种类型作为表达式 Payload.cc(22):参见“spectralFrameWord”的声明 Payload.cc(77):错误 C2228:“.words”左侧必须有类/结构/联合

代码

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

【问题讨论】:

  • 显示错误中引用的行(77 和 22,以及上下文周围的行)。

标签: c++ visual-studio-2013


【解决方案1】:

你写的

spectralFrameWord[0].words

但是意思

spectralFrameWords[0].words

编译器无法让这更清楚。它说:

'.words' 的左边必须有类/结构/联合

这告诉您检查words 左侧的内容,看看为什么它不符合要求。


当您使用new sometype[...] 分配时,您必须使用delete[] 解除分配。使用std::vector&lt;T&gt; 可能更合适。

【讨论】:

    【解决方案2】:

    我认为有一个错字。至少在这里而不是

    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]);
    }
    

    使用仅在一个符号上不同的名称是一种非常糟糕的做法。

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      相关资源
      最近更新 更多