C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

creating map<string, Object> question: how to make map with object as value Rate Topic: -----

#1 carsten_dk  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-October 07


Dream Kudos: 0

Share |

creating map<string, Object>

Post icon  Posted 27 October 2007 - 08:52 AM

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

Was This Post Helpful? 0
  • +
  • -


#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • Icon

Reputation: 1005
  • View blog
  • Posts: 7,698
  • Joined: 18-February 07


Dream Kudos: 1250

Expert In: Java/C++

Re: creating map<string, Object>

Posted 28 October 2007 - 08:52 AM

Quote

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.


Ok, your first statement is probably correct in that C++ does not have this idea that all Objects are derived from a class called Object. But in you code I don't see that you tried anything like that.

What do you mean by "unnamed versions of Opcode"?

What exactly are your troubles?


Do you mean to just put "any old object" as the value of the map? I don't know how well this would work in C++ without RTTI. Maybe if I understood your base problem a little better.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • Icon

Reputation: 1005
  • View blog
  • Posts: 7,698
  • Joined: 18-February 07


Dream Kudos: 1250

Expert In: Java/C++

Re: creating map<string, Object>

Posted 28 October 2007 - 08:58 AM

BTW, you can make a map<string, void *> that will store just about anything. but you need RTTI to find out what type of object the pointer is pointing at.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users