I am getting an unqualified ID error on lines 89 and 96 which, in the code are:
ID: 3850082 • Letter: I
Question
I am getting an unqualified ID error on lines 89 and 96 which, in the code are:
Actor.info = attribute;
and
Actor.entity info -> name = attribute -> name;
Can someone help me figure out why?
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<map>
#include<vector>
void tokenize(const std::string& str,
std::vector<std::string>& tokens,
const std::string& delimiters = " ") {
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
class Actor;
class InvItem;
typedef std::map<std::string,Actor*> ActorMap;
typedef std::vector<InvItem> InvBag;
typedef std::vector<std::string> TokenVector;
std::ostream& operator<<(std::ostream& o,TokenVector v) {
for(int i=0; i<v.size(); i++)
o << v[i] << " ";
return o;
}
typedef struct {
int health,manna,stamina,intel;
} VitalStruct;
typedef struct {
std::string name, type;
} EntityInfo;
std::ostream& operator<<(std::ostream& o,VitalStruct v) {
o << "health:" << v.health << std::endl;
o << "manna:" << v.manna << std::endl;
o << "stamina:" << v.stamina << std::endl;
o << "intel:" << v.intel << std::endl;
return o;
}
class Actor {
protected:
VitalStruct vitals;
InvBag inventory;
EntityInfo info;
public:
VitalStruct get_vitals();
InvBag get_inventory();
EntityInfo set_type(const EntityInfo&);
EntityInfo get_type();
EntityInfo set_name(const EntityInfo&);
EntityInfo get_name();
void set_inventory(const InvBag&);
virtual void speak(const std::string &message)=0;
};
class InvItem {
};
InvBag& operator<<(InvBag& bag,InvItem iv) {
bag.push_back(iv);
return bag;
}
EntityInfo Actor::get_type() {
return info;
}
EntityInfo Actor::set_type(const EntityInfo& attribute) {
Actor.info = attribute;
}
EntityInfo Actor::get_name() {
return info;
}
EntityInfo Actor::set_name(const EntityInfo& attribute) {
Actor.entity info -> name = attribute -> name;
}
VitalStruct Actor::get_vitals() {
return vitals;
}
void Actor::set_inventory(const InvBag& ivbag) {
inventory = ivbag;
}
InvBag Actor::get_inventory() {
return inventory;
}
class Monster : public Actor {
public:
void speak(const std::string& message);
};
void Monster::speak(const std::string& message) {
std::cout << "Monster: " << message << std::endl;
}
class Hero : public Actor {
void speak(const std::string& message);
};
void Hero::speak(const std::string& message) {
std::cout << "Hero: " << message << std::endl;
}
class Villager : public Actor {
void speak(const std::string& message);
};
void Villager::speak(const std::string& message) {
std::cout << "Villager: " << message << std::endl;
}
int main() {
ActorMap actormap;
InvBag bag;
EntityInfo type;
EntityInfo name;
std::ifstream in_file("./characters.txt");
if(in_file.fail()) {
std::cout << "Cannot open file";
exit(1);
}
std::string buf="";
while(!in_file.eof()) {
std::getline(in_file,buf);
if(buf.at(0) == '-') {
break;
}
TokenVector tokens;
tokenize(buf,tokens,":");
std::string name = tokens[0];
std::string type = tokens[1];
if(type == "Hero") {
actormap[name] = new Hero();
//set name
//set type
}
else if(type == "Monster") {
actormap[name] = new Monster();
}
else if(type == "Villager") {
actormap[name] = new Villager();
}
actormap[name]->speak("I speak");
actormap[name]->get_name();
}
/*
actormap["trundle"] = new Monster();
actormap["tim"] = new Hero();
actormap["penelope"] = new Villager();
bag << InvItem();
actormap["trundle"]->set_inventory(bag);
std::cout << actormap["trundle"]->get_vitals();
actormap["trundle"]->speak("I am trundle, prepare to meet your maker!");
actormap["penelope"]->speak("Where's my hero?");
actormap["tim"]->speak("Let's roll!");
*/
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<map>
#include<vector>
void tokenize(const std::string& str,
std::vector<std::string>& tokens,
const std::string& delimiters = " ") {
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
class Actor;
class InvItem;
typedef std::map<std::string,Actor*> ActorMap;
typedef std::vector<InvItem> InvBag;
typedef std::vector<std::string> TokenVector;
std::ostream& operator<<(std::ostream& o,TokenVector v) {
for(int i=0; i<v.size(); i++)
o << v[i] << " ";
return o;
}
typedef struct {
int health,manna,stamina,intel;
} VitalStruct;
typedef struct {
std::string name, type;
} EntityInfo;
std::ostream& operator<<(std::ostream& o,VitalStruct v) {
o << "health:" << v.health << std::endl;
o << "manna:" << v.manna << std::endl;
o << "stamina:" << v.stamina << std::endl;
o << "intel:" << v.intel << std::endl;
return o;
}
class Actor {
protected:
VitalStruct vitals;
InvBag inventory;
EntityInfo info;
public:
VitalStruct get_vitals();
InvBag get_inventory();
EntityInfo set_type(const EntityInfo&);
EntityInfo get_type();
EntityInfo set_name(const EntityInfo&);
EntityInfo get_name();
void set_inventory(const InvBag&);
virtual void speak(const std::string &message)=0;
};
class InvItem {
};
InvBag& operator<<(InvBag& bag,InvItem iv) {
bag.push_back(iv);
return bag;
}
EntityInfo Actor::get_type() {
return info;
}
EntityInfo Actor::set_type(const EntityInfo& attribute) {
Actor.info = attribute;
}
EntityInfo Actor::get_name() {
return info;
}
EntityInfo Actor::set_name(const EntityInfo& attribute) {
Actor.entity info -> name = attribute -> name;
}
VitalStruct Actor::get_vitals() {
return vitals;
}
void Actor::set_inventory(const InvBag& ivbag) {
inventory = ivbag;
}
InvBag Actor::get_inventory() {
return inventory;
}
class Monster : public Actor {
public:
void speak(const std::string& message);
};
void Monster::speak(const std::string& message) {
std::cout << "Monster: " << message << std::endl;
}
class Hero : public Actor {
void speak(const std::string& message);
};
void Hero::speak(const std::string& message) {
std::cout << "Hero: " << message << std::endl;
}
class Villager : public Actor {
void speak(const std::string& message);
};
void Villager::speak(const std::string& message) {
std::cout << "Villager: " << message << std::endl;
}
int main() {
ActorMap actormap;
InvBag bag;
EntityInfo type;
EntityInfo name;
std::ifstream in_file("./characters.txt");
if(in_file.fail()) {
std::cout << "Cannot open file";
exit(1);
}
std::string buf="";
while(!in_file.eof()) {
std::getline(in_file,buf);
if(buf.at(0) == '-') {
break;
}
TokenVector tokens;
tokenize(buf,tokens,":");
std::string name = tokens[0];
std::string type = tokens[1];
if(type == "Hero") {
actormap[name] = new Hero();
//set name
//set type
}
else if(type == "Monster") {
actormap[name] = new Monster();
}
else if(type == "Villager") {
actormap[name] = new Villager();
}
actormap[name]->speak("I speak");
actormap[name]->get_name();
}
/*
actormap["trundle"] = new Monster();
actormap["tim"] = new Hero();
actormap["penelope"] = new Villager();
bag << InvItem();
actormap["trundle"]->set_inventory(bag);
std::cout << actormap["trundle"]->get_vitals();
actormap["trundle"]->speak("I am trundle, prepare to meet your maker!");
actormap["penelope"]->speak("Where's my hero?");
actormap["tim"]->speak("Let's roll!");
*/
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.