Basic VM code wont compile

I am building a basic VM and my code will not compile, get the error
"In file included from Main.cpp:2:
Instruction.hpp:15:2: error: expected ‘;’ after class definition " But there is a ; there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <cstdint>

	enum Opcode : uint8_t
	{
		EXIT,
		ADD_INT,
		PUSH_INT,
		PRINT_INT
	};

	class Instruction{
	public:
		Opcode opcode;
		uint16_t  op1;
	};
What compiler are you using and ror what version of C++ are you compiling?

This compiles OK using VS2019:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdint>

enum Opcode : uint8_t
{
	EXIT,
	ADD_INT,
	PUSH_INT,
	PRINT_INT
};

class Instruction {
public:
	Opcode opcode;
	uint16_t  op1;
};

Hey,
I am using g++ and C++ 14
Does this code come from a header file? Is it being included in a source file that has other headers included above it? Could the actual error be caused by one of those earlier headers?
Yes it comes from a header file. It is included in my Main.cpp file
1
2
3
4
5
6
7
#include "Instruction.hpp"
#include <iostream>

int main() {
    Instruction* pc;
    return 0;
}

I do not think that any other file causes this error. I switched the include statements now I get to following compiler error:
"Instruction.hpp:14:2: error: ‘uint16_t’ does not name a type
14 | uint16_t op1;
| ^~~~~~~~
Instruction.hpp:15:2: error: expected ‘;’ after class definition
15 | }
"
Seems fine on clang as C++14 and 17.

One thing ...
If you're using <cstdint>, you probably should be using std::uint8_t.

There's nothing to stop you from using <stdint.h>
Topic archived. No new replies allowed.