Question

Ok so I am trying to map some of my member functions in the .h file this is to be able to use the map when I implement the code. However, after hours I have gotten nowhere so I would like suggestions or if anyone knows how to implement this. For reference these are the errors.

./Assembler.h:51:2: error: C++ requires a type specifier for all declarations
    functions["load"] = load;
    ^~~~~~~~~
./Assembler.h:51:12: error: size of array has non-integer type 'const char [5]'
    functions["load"] = load;
              ^~~~~~
./Assembler.h:51:2: error: duplicate member 'functions'
    functions["load"] = load;
    ^

As for my header file it with the problem coming from the map:

#include <vector>
#include <iostream>
#include <map>
#include <string>
#include <fstream>

using namespace std;

class Assembler {
public:

Assembler(string filename);//Argument will be passed from the os.cpp file
void parse();// Will go through the a file to output the .o file
void load();
void loadi();
void store();
void add();
void addi();
void addc();
void addci();
void sub();
void subi();
void subc();
void subci();
void ander();
void andi();
void xorer();
void xori();
void negate();
void shl();
void shla();
void shr();
void shra();
void compr();
void compri();
void getstat();
void putstat();
void jump();
void jumpl();
void jumpe();
void jumpg();
void call();
void ret();
void read();
void write();
void halt(); 
void noop();

private:
typedef void (*function)();
map<string, function> functions;
functions["load"] = load;
fstream in, out; //One will be the .s file while the other will be the .o file 
string opcode;
int rd, rs, constant, addr, machcode; //Different parts of the instruction
};

Any help or suggestions would be appreciated. Thanks

Was it helpful?

Solution

Only static const integral data members can be initialized within a class. You probably need to move functions["load"] = load; to a function's definition.

And also, you need to change them to:

typedef void (Assembler::*function)();
...
functions["load"] = &Assembler::load;

OTHER TIPS

Within C++ class declaration, you cannot have member initialiser or executable statement, Have this one

functions["load"] = load;

within constructor

Take a look at your class declaration: everything's compiling fine except

functions["load"] = load;

This is an assignment and initializes the functions map with something. That is not allowed in the declaration which is a "contract" (in the case of an interface) or "explanation" of how your class is composed and what methods/members has.

The right spot to put such an initialization is in your constructor's definition (i.e. in the part of the code that actually contains the code of your methods, specifically when the object gets created if you intend to initialize stuff.. i.e. the constructor).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top