I have created a class Opcode and OpcodeList. I want OpcodeList to create a map of Opcodes. The data is drawn from a file, which i have just tested in an output file - hence the out << line << endl; please ignore those.
My problem is with: opcodeMap<string, Opcode> in OpcodeList.cpp
Probably what i have tried is a java programmers atempt to adapt to c++
I canīt figure out how to create unnamed versions of Opcode and put them in the map.
Thanks you
Carsten
OpcodeList.cpp:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "OpcodeList.h"
#include "Opcode.h"
using namespace std;
//default constructor
OpcodeList::OpcodeList()
{
}
void OpcodeList::setOpcodeList(string dataFile)
{
//intialize
string line;
string data = dataFile;
ifstream file;
ofstream out;
string name;
// Throw exceptions whenever "failbit" or "badbit" gets set
//file.exceptions (ifstream::failbit | ifstream::badbit);
try{
//Open opcodes list
file.open(data.c_str());
//open outfile
out.open("c:\\opcodesout.txt");
//get all data while not eof
while (file){
getline(file, line);
if(!line.substr(0,6).compare("name: ")){
OpcodeList::opcodeMap[name] = new Opcode::Opcode opcode;
}
else if(!line.substr(0, 13).compare("description: ")){
out << line.substr(13, string::npos) << endl;
}
else if(!line.substr(0, 8).compare("output: ")){
out << line.substr(8, string::npos) << endl;
}
else if(!line.substr(0, 9).compare("command: ")){
out << line.substr(9, string::npos) << endl;
}
else if(!line.substr(0, 7).compare("input: ")){
out << line.substr(7, string::npos) << endl;
}
else if(!line.substr(0, 10).compare("optional: ")){
out << line.substr(10, string::npos) << endl;
}
else {
cout << "line tag not recognized in line:" << endl;
cout << line << endl;
}
}
}
catch (ifstream::failure e){
cout << "error: " << e.what() << endl;
}
file.close();
out.close();
}
OpcodeList.h:
#ifndef _OPCODELIST_H
#define _OPCODELIST_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Opcode.h"
using namespace std;
class OpcodeList
{
public:
//Default Constructor
OpcodeList();
static std::map<string, Opcode> opcodeMap;
//mutators
void setOpcodeList(string dataFile);
//Accessors
protected:
private:
};
#endif /* _OPCODELIST_H */
Opcode.h:
#ifndef _OPCODE_H
#define _OPCODE_H
#include <iostream>
#include <string>
using namespace std;
class Opcode
{
public:
//Default Constructor
Opcode();
string name;
string description;
vector<string> output;
vector<string> command;
vector<string> input;
vector<string> optional;
//Mutators
void setName();
void setDescription();
void setOutput();
void setCommand();
void setInput();
void setOptional();
//Accessors
protected:
private:
};
#endif /* _OPCODE_H */
OpcodeListTester.cpp:
#include "OpcodeList.h"
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
OpcodeList opcodeList;
opcodeList.setOpcodeList("c:\\opcodes.txt");
return (EXIT_SUCCESS);
}

Ask A New Question
Reply





MultiQuote




|