1

My program won't allow me to output these values because the "type name is not allowed" (highlighted in asterisks). What does that mean? What's wrong? I'm trying to parse this MIDI file, and it seems I need to use these data types, as the program was outputting the incorrect values (only MThd was outputting correctly, the rest were random hex values). What can I do to fix this? I'm new to coding, so sorry if this is a stupid question.

#include <iostream>
#include <fstream>
#include <stdint.h>

typedef struct MIDI_CHUNK_HEADER {
    char               MThd[4];
    typedef uint32_t   ChunckSize[4];
    typedef uint16_t   Format[2];
    typedef uint16_t   Track[2];
    typedef uint16_t   TimeDivision[2];
} MIDI_CHUNK_HEADER;

int main()
{
    std::ifstream In_File("C:\\Users\\micah\\Documents\\Twinkle.mid", std::ios::binary | std::ios::in);
    if (!In_File)
    {
        std::cerr << "Problem opening file!";
        return 1;
    }

    MIDI_CHUNK_HEADER midi_chunk_header;
    In_File.read((char*)&midi_chunk_header, sizeof(MIDI_CHUNK_HEADER));
    std::cout << "File type is: "<< midi_chunk_header.**MThd** << std::endl;
    std::cout << "Bytes to follow: " << midi_chunk_header.**ChunckSize** << std::endl;
    std::cout << "MIDI Format: " << midi_chunk_header.**Format** << std::endl;
    std::cout << "MIDI Track: " << midi_chunk_header.**Track** << std::endl;
    std::cout << "MIDI Time Division: " << midi_chunk_header.**TimeDivision** << std::endl;
}
MadLad0913
  • 13
  • 3

1 Answers1

2

typedef declares an alias for a type name. It does not declare a variable. You cannot use a type name in an expression as if it was a variable name.

If you intended to declare class members, then drop the typedef. Also don't use typedef struct in C++. It has use in C, but is redundant in C++. A class can be defined directly without typedef:

struct MIDI_CHUNK_HEADER {
    char       MThd[4];
    uint32_t   ChunckSize[4];
    uint16_t   Format[2];
    uint16_t   Track[2];
    uint16_t   TimeDivision[2];
};

Furthermore you cannot output arrays directly with <<. It will print the address of the first array element instead, except for the case of char arrays (which will be interpreted as C style null-terminated strings). You need to loop over the individual elements and output each individually.


(Without looking at the MIDI chunk header format specification:) It seems that you don't actually want arrays at all. If you want e.g. ChunckSize to represent 4 bytes, then you want only one uint32_t (which is 32bits equaling 4 bytes already by itself), not four of them:

struct MIDI_CHUNK_HEADER {
    char       MThd[4];
    uint32_t   ChunckSize;
    uint16_t   Format;
    uint16_t   Track;
    uint16_t   TimeDivision;
};

Please learn the language from a good book. It would explain how everything works in a structured way and you wouldn't make mistakes such as using typedef here. Learning C++ in an unstructured way will not get you far. C++ is too unforgiving to mistakes.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Thank you so much! I'm right now learning through a Udemy course, but haven't considered getting a book yet (will do!). As for why I need arrays, it's because I need to read specific bits (delta time duration) for the MIDI note on and off events (plus the time division). You have been very helpful :) – MadLad0913 Jan 07 '20 at 04:51