weird typing mismatch c++ - c++

So I am not sure why I am getting this error when in the .h files it was defined that format was a Parser*. The compiler tells me that in the method Parser::changeformat(int) I cannot convert an int* to a Parser* pointer(the cpp files aren't done yet so please ignore all the other methods if they throw errors or look weird). Why is this happening and how can I fix it. This may have something to do with inheritance so I'll point out a subclass of parser that would fit. Also, in any case if you have suggestions about other parts then be my guest.
Parser.h
#ifndef PARSER_H_
#define PARSER_H_
#include <string>
#include <iostream>
#include <fstream>
/*
* This is a parser for reading and writing files
* it takes in an integer for the file type it should read
* or none if you would like to change it later and reuse
* the parser
*/
class Parser {
public:
Parser();
Parser(int);
virtual ~Parser();
void open();
bool open(std::string&);
bool read();
bool write();
bool close();
void changeformat(int);
private:
int filetype = -1;
Parser* format = 0;
};
#endif /* PARSER_H_ */
Parser.cpp
#include "Parser.h"
Parser::Parser()
{
filetype = -1;
}
Parser::Parser(int filetype)
{
switch(filetype)
{
case 0:
{
//load xml format via instantiating xmlpar subclass and overloading methods
break;
}
case 1:
{
//load txt format
break;
}
}
}
Parser::~Parser()
{
if(this->format)
delete this->format;// TODO Auto-generated destructor stub
}
//the classes below are to be overloaded with a subclass's own method
void Parser::open()
{
return;
}
bool Parser::open(std::string& filename)
{
if(this->format->open(filename))
{
std::cout<<"OK: "+filename+" opened\n";
return true;
}
else
{
std::cout<<"Error: "+filename+" unable to be opened\n";
return false;
}
}
bool Parser::read()
{
//make failure checks for all past open in this cpp
return this->format->read();
}
bool Parser::write()
{
return this->format->write();
}
bool Parser::close()
{
return this->format->close();
}
void Parser::changeformat(int)
{
switch(filetype)
{
case -1:
break;
case 0:
{
this->format = new xmlpar();
break;
}
case 1:
{
//load txt format
break;
}
}
}
xmlpar.h
/*
* xmlpar.h
*
* Created on: Jul 22, 2015
* Author: root
*/
#ifndef XMLPAR_H_
#define XMLPAR_H_
#include "Parser.h"
#include <fstream>
#include <string>
class xmlpar: public Parser{
public:
xmlpar();
virtual ~xmlpar();
bool open(std::string&);//opens a stream and checks association
bool read(std::fstream&);//creats dom tree and hands it forward via reference
bool write(std::fstream&);//edits domtree but does not write to the physical file
bool close(std::fstream&);//the dom tree is flushed, the fstream associated to the file is closed and everyone is happy... I think
private:
std::fstream *file= 0;
bool flush();//write dom tree in memory to physical file
};
#endif /* XMLPAR_H_ */
xmlpar.cpp
/*
* xmlpar.cpp
*
* Created on: Jul 22, 2015
* Author: root
*/
#include "xmlpar.h"
xmlpar::xmlpar()
{
}
bool xmlpar::open(std::string& filename)
{
file = new std::fstream(filename, std::ios::in|std::ios::out);
return file->good();
}
bool xmlpar::close(std::fstream &file)
{
this->write(file);
file->close();
//write failcheck here
}
xmlpar::~xmlpar() {
this->close(file);
}

You are trying to instantiate an instance of xmlpar that is derived from Parser base class without including headers of derived class xmlpar at Parser.cpp. The compiler is not finding definition for xmlpar here:
this->format = new xmlpar();
I also doubt this is a good practice.

Related

Member function doesn't work when using pointer to class

Scenario: I have two classes, each contains a pointer to the other (when using them, being able to refer to the other is going to be important so I deemed this appropriate). When I try accessing a private variable from one class via using the pointer to the other and a getter function inside that, it works perfectly.
Problem: Using a setter (in this case, addPoints)/manipulating the variables however leads to no result.
I'm new so anything here might be "improper etiquette" and bad practice. Feel free to point them out! But please also try to provide a solution. This is also my first question on SO, so please be gentle!
Related code pieces:
Team.h
#include "Driver.h"
using namespace std;
class Team {
int Points = 0;
vector<Driver*> Drivers;
public:
void addPoints(int gained); //does not work
int getPoints(); //works perfectly
Driver getDriver(int nr);
void setInstance(vector<Driver*> drivers);
};
Team.cpp
#include "Team.h"
#include "Driver.h"
using namespace std;
void Team::addPoints(int gained) {
this->Points = this->Points + gained;
}
int Team::getPoints() {
return this->Points;
}
Driver Team::getDriver(int nr) {
return *Drivers[nr];
}
void Team::setInstance(vector<Driver*> drivers) {
this->Drivers = drivers;
}
Driver.h
using namespace std;
class Team;
class Driver {
int Points = 0;
Team* DriversTeam;
public:
void SetTeam(Team& team);
Team getTeam();
int getPoints(); //works
void addPoints(int gained); //doesn't work
};
Driver.cpp
#include "Driver.h"
#include "Team.h"
using namespace std;
void Driver::SetTeam(::Team& team) {
this->DriversTeam = &team;
}
Team Driver::getTeam() {
return *DriversTeam;
}
int Driver::getPoints() {
return this->Points;
}
void Driver::addPoints(int gained) {
this->Points = this->Points + gained;
}
Initializer.cpp (linking drivers to teams)
void InitializeData(vector<Team>& teams, vector<Driver> &drivers) {
//(...)
//reads each team in from data file to memory
//key part:
vector<Driver*> teamsDrivers;
for (auto& iter : drivers) { //this loop mainly determines which driver to link with which teams
if (iter.getName().compare(values[4]) == 0) { //values is csv line data in a string vector. I guess not the prettiest parsing method here but will be revised
teamsDrivers.push_back(&iter);
}else if(iter.getName().compare(values[5]) == 0) {
teamsDrivers.push_back(&iter);
}
}
tempTeam.setInstance(teamsDrivers);
teams.push_back(tempTeam);
}
(linking driver to team)
//drivers are linked to teams last, as they are declared first (so I cannot link them to the yet nonexisting teams)
void LinkTeam(vector<Driver>& drivers, vector<Team>& teams) {
for (auto& driverIter : drivers) { //iterate through drivers
for (auto& teamIter : teams) { // iterate through teams
bool found = 0;
for (size_t i = 0; i < teamIter.DriverAmount(); i++) {
if (driverIter.getName() == teamIter.getDriver(i).getName()) {
driverIter.SetTeam(teamIter);
found = 1;
break;
}
}
if (found) { //exit iterating if driver is found
break;
}
}
}
}
Example of use in main.cpp
teams[0].addPoints(10);
drivers[3].getTeam().addPoints(15); //driver 3 is linked to team 0
cout << teams[0].getPoints(); //15
cout << drivers[3].getTeam().getPoints(); //15
teams[0].getDriver(1).addPoints(20); //driver 1 of team 0=driver[3]
drivers[3].addPoints(25);
cout << drivers[3].getPoints(); //25
cout << teams[0].getDriver(1).getPoints(); //25
Thanks for the help in advance.
This is quite simple:
Your getTeam() and getDriver() functions are returning copies of the objects, not references, so the addPoints() are performed on temporary copies and not the real ones.
To fix it, simply change the return types to references (add &):
Team& getTeam();
and
Driver& getDriver();

PlatformIO Unidentified reference error to defined attributes

I am creating a project using PlatformIO and a Nodemcuv2 micro-controller.
I have written a class for serial communication SerialCommunicationHandler. This class ICommunicationHandler implements a Interface. See the code below.
ICommunicationHandler.h
class ICommunicationHandler {
public:
virtual void sendTemperature(float temp) = 0;
virtual void receiveData() = 0;
virtual void update() = 0;
protected:
virtual void parseData() = 0;
virtual void showParsedData() = 0;
};
SerialCommunicationHandler
headerfile
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
bool receiving;
bool newData;
void parseData() override;
void receiveData() override;
void showParsedData() override;
public:
explicit SerialCommunicationHandler();
void sendTemperature(float temp) override;
void update() override;
};
.cpp
#include <Arduino.h>
#include "SerialCommunicationHandler.h"
SerialCommunicationHandler::SerialCommunicationHandler() {
messagePosition = 0;
receiving = false;
newData = false;
}
void SerialCommunicationHandler::receiveData() {
//check if there are bytes in the serial buffer.
while (Serial.available() > 0){
char inByte = Serial.read();
//check if the byte is a starting or ending character;
switch (inByte) {
case '<':
//start receiving characters
receiving = true;
break;
case '>':
//stop receiving and parse the incoming data.
receiving = false;
newData = true;
strcpy(receivedData, incomingData); //copy incoming data into receivedData for further parsing.
memset(incomingData, 0, sizeof(incomingData)); //resetting incomingData.
messagePosition = 0;
break;
default:
if (receiving) {
incomingData[messagePosition] = inByte; //add incoming byte to array.
messagePosition++;
}
break;
}
}
}
void SerialCommunicationHandler::parseData() {
if (newData) {
showParsedData();
}
newData = false;
}
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
void SerialCommunicationHandler::sendTemperature(float temp) {
Serial.println(temp);
}
void SerialCommunicationHandler::update() {
receiveData();
parseData();
}
When building I get multiple undefined refernce errors:
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler14s
howParsedDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12receivedDataE`
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandlerC2E
v+0x4): undefined reference to `_ZN26SerialCommunicationHandler15messagePositionE'
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler11r
eceiveDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12incomingDataE'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcuv2\firmware.elf] Error 1
I have checked my code multiple times for syntax errors or misspelling but have found nothing. My IDE doesnt bring up any errors as well. Any information on what might be causing the undefined reference error is welcome.
headerfile
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
// ... etc
These are only declarations of the static member variables. You also have to define them in the cpp file, just like you do with the member functions:
.cpp file:
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
// static data members
char SerialCommunicationHandler::incomingData[6] = {};
char SerialCommunicationHandler::receivedData[6] = {};
unsigned int SerialCommunicationHandler::messagePosition = 0;

Writing to a file from multiple functions and replacing the values C++

I have a file example.txt which will be updated with values by different functions at seperate lines. And the mentioned three functions will be in a timer loop so it will continuously calling the three functions again and again so the three functions will need to write the file with their updated values calculated in it.
For better understanding please see the example scenario,
First time when the functions called,
*Function1 is writing value of 1 at first line of the file.
*Function2 is writing value of 5 at second line of the file.
*Function3 is writing value of 7 at third line of the file.
Now again timer function calling the above three functions again and i am expecting the below output in the file to be written.
*Function1 should write value of 8 at first line of the file.
*Function2 should write value of 9 at second line of the file.
*Function3 should write value of 10 at third line of the file.
Sample program :
#include <iostream>
#include <fstream>
using namespace std;
void writeToFile(int seq)
{
fstream myfile;
myfile.open("example.txt",fstream::in | fstream::out | fstream::app );
myfile << seq <<endl;
myfile.close();
}
void A()
{
writeToFile(5);
}
void B()
{
writeToFile(2);
}
void C()
{
writeToFile(4);
}
//assume timer function called for every 20secs;
void timer_func()
{
A();
B();
C();
}
int main()
{
timer_func();
return 0;
}
Please suggest me some possible ways to attain it.
Consider keeping the file in memory in a class and writing it as soon as something changes. And remember thread safety, if applicable.
It is bad idea to do things in that way, but if you want so...
#include <string> // string
#include <fstream> // fstream
#include <cstdio> // snprintf
#include <cassert> // assert
#include <cstring> // memset
#define INITIAL_LINE_COUNT 3
#define INITIAL_FILE_NAME "example.txt"
// singleton class that provides writing to the file
class LineWriter
{
private:
std::fstream m_file;
int m_line_count;
// common singleton implementation
LineWriter() = delete;
~LineWriter() = default;
LineWriter(const LineWriter&) = delete;
LineWriter& operator = (const LineWriter&) = delete;
LineWriter(LineWriter&&) = delete;
LineWriter& operator = (LineWriter&&) = delete;
LineWriter(const std::string& filename, int line_count)
{
m_file.exceptions(std::fstream::badbit);
m_file.open(
filename,
std::fstream::out | std::fstream::binary
);
// fill the file with empty 16-byte length strings ended by CRLN
{
for (int i = 0; i < line_count; ++i)
{
m_file.write(" \r\n", 16);
}
}
m_line_count = line_count;
}
public:
void write(int line, int value)
{
// just to check that everything is ok
assert(m_file.is_open());
assert(line <= m_line_count && line > 0);
char buffer[14];
int size = std::snprintf(buffer, 14, "%d", value);
// calculate position of line
// just simple (line - 1) * 16
int pos = (line - 1) << 4;
// clear previous value
m_file.seekp(pos);
m_file.write(" ", 14);
// write new one
m_file.seekp(pos);
m_file.write(buffer, size);
}
static LineWriter& get_instance()
{
static LineWriter instance(INITIAL_FILE_NAME, INITIAL_LINE_COUNT);
return instance;
}
};
void write_to_file(int line, int value)
{
LineWriter::get_instance().write(line, value);
}
void call_a() { write_to_file(1, 5); }
void call_b() { write_to_file(2, 13); }
void call_c() { write_to_file(3, 2); }
void timer_func()
{
call_a();
call_b();
call_c();
}
int main()
{
while (true)
{
timer_func();
}
}
As mentioned #n.m. it is much better to just cache result of all functions and write them single time (if there are not so many lines).
If there are a lot of values and strings, so it is better to use database engines like sqlite as mentioned #JesperJuhl in coments.
Also this example is not thread safe, if you need call this writer from multiple threads just add member mutex lock at the top of LineWriter::write method:

Find If-Conditions using Clang

I'm trying to find the if-conditions in a C-code using Clang.
What I've learned till now is to find the declarations using HandleTopLevelDecl().
What I'm trying to do now is to find a function that is similar to HandleTopLevelDecl() but handles the If-Conditions.
My question is, am I on the right path? is there a function that can do this?
And if not, what do you advice me to do?
Thanks.
With the help of this awesome course: http://swtv.kaist.ac.kr/courses/cs453-fall13
Specially this tutorial: http://swtv.kaist.ac.kr/courses/cs453-fall13/Clang%20tutorial%20v4.pdf
I have solved the problem.
I needed to create a RecursiveASTVisitor and handle the If-Statements while visiting the Statements.
class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
{
public:
bool VisitStmt(Stmt *s) {
// Search for If-Statements
if(isa<IfStmt>(s))
{
cerr << "Found IF" << endl;
}
return true;
}
bool VisitFunctionDecl(FunctionDecl *f) {
// Print function name
cerr << f->getNameAsString().c_str() << endl;
return true;
}
};
And here are the complete code:
#include <cstdio>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <utility>
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace std;
// CompilerInstance
CompilerInstance TheCompInst;
class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
{
public:
bool VisitStmt(Stmt *s) {
// Search for If-Statements
if(isa<IfStmt>(s))
{
SourceManager &srcmgr = TheCompInst.getSourceManager();
SourceLocation startLocation = s->getLocStart();
unsigned int start_lineNum = srcmgr.getExpansionLineNumber(startLocation);
cerr << "Found IF # Line: " << start_lineNum << endl;
}
return true;
}
bool VisitFunctionDecl(FunctionDecl *f) {
// Print function name
cerr << f->getNameAsString().c_str() << endl;
return true;
}
};
class MyASTConsumer : public ASTConsumer
{
public:
MyASTConsumer()
: Visitor() //initialize MyASTVisitor
{}
virtual bool HandleTopLevelDecl(DeclGroupRef DR) {
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
// Travel each function declaration using MyASTVisitor
Visitor.TraverseDecl(*b);
}
return true;
}
private:
MyASTVisitor Visitor;
};
int main(int argc, char *argv[])
{
if (argc != 2) {
llvm::errs() << "Usage: kcov-branch-identify <filename>\n";
return 1;
}
// Diagnostics manage problems and issues in compile
TheCompInst.createDiagnostics(NULL, false);
// Set target platform options
// Initialize target info with the default triple for our platform.
TargetOptions *TO = new TargetOptions();
TO->Triple = llvm::sys::getDefaultTargetTriple();
TargetInfo *TI = TargetInfo::CreateTargetInfo(TheCompInst.getDiagnostics(), TO);
TheCompInst.setTarget(TI);
// FileManager supports for file system lookup, file system caching, and directory search management.
TheCompInst.createFileManager();
FileManager &FileMgr = TheCompInst.getFileManager();
// SourceManager handles loading and caching of source files into memory.
TheCompInst.createSourceManager(FileMgr);
SourceManager &SourceMgr = TheCompInst.getSourceManager();
// Prreprocessor runs within a single source file
TheCompInst.createPreprocessor();
// ASTContext holds long-lived AST nodes (such as types and decls) .
TheCompInst.createASTContext();
// A Rewriter helps us manage the code rewriting task.
Rewriter TheRewriter;
TheRewriter.setSourceMgr(SourceMgr, TheCompInst.getLangOpts());
// Set the main file handled by the source manager to the input file.
const FileEntry *FileIn = FileMgr.getFile(argv[1]);
SourceMgr.createMainFileID(FileIn);
// Inform Diagnostics that processing of a source file is beginning.
TheCompInst.getDiagnosticClient().BeginSourceFile(TheCompInst.getLangOpts(),&TheCompInst.getPreprocessor());
// Create an AST consumer instance which is going to get called by ParseAST.
MyASTConsumer TheConsumer;
// Parse the file to AST, registering our consumer as the AST consumer.
ParseAST(TheCompInst.getPreprocessor(), &TheConsumer, TheCompInst.getASTContext());
return 0;
}
There are specific functions in clang for all kinds of Stmts and Decls. For this particular case, it is going to be VisitIfStmt. Check IfStmt being included in RecusiveASTVisitor.h.
RecursiveASTVisitor.h -
#define STMT(CLASS, PARENT) \
bool WalkUpFrom##CLASS(CLASS *S) { \
TRY_TO(WalkUpFrom##PARENT(S)); \
TRY_TO(Visit##CLASS(S)); \
return true; \
} \
bool Visit##CLASS(CLASS *S) { return true; }
#include "clang/AST/StmtNodes.inc"
clang/AST/StmtNodes.inc -
#ifndef IFSTMT
# define IFSTMT(Type, Base) STMT(Type, Base)
#endif
IFSTMT(IfStmt, Stmt)
#undef IFSTMT
These, together create the function VisitIfStmt(IfStmt*) in the class.

Error assigning object to pointer c++

Me and my friend are making a text based game in c++ for fun, and to learn a little more. I have been trying to use pointers to classes, but am having no luck, and some errors are occurring which make absolutely no sense to me at all, and am hoping someone can help me.
Code:
//Map.h
#include "Player.h"
class Map
{
//Virtual functions
};
class StartMap : public Map
{
//Code
}Start;
class JungleMap : public Map
{
//Code
}Jungle;
class RiverMap : public Map
{
//Code
}River;
//Player.h
#ifndef MAP_H
#define MAP_H
#endif
class Player
{
private:
Map *PlayerMap;
//Other variables
public:
void Initialize()
{
//Initialize variables
PlayerMap = &Start; //This is where the error occurs, says there's a
//<error-type>*Player::PlayerMap. Tried putting
//this->PlayerMap = &Start, didn't help
//There's no error when I make the pointer
}
//Bunch of other functions
}Player;
Okay, here's my code since I decided to add .cpp files:
//Command.h
class Command
{
private:
string GameCommand;
void Trim();
public:
Command (string command) {GameCommand = command;}
Command () {}
void operator = (string command) {GameCommand = command;}
void ReadCommand();
string Print();
}
//Command.cpp
#include <iostream>
#include <string>
#include "Command.h"
#include "Parameter.h"
using namespace std;
void Command::Trim()
{
int LeadingPos = 0, MidCount = 0, TrailingPos = GameCommand.length()-1, Size = 0;
string TempCommand = "";
while (GameCommand[LeadingPos] == ' '){LeadingPos += 1;}
while (GameCommand[TrailingPos] == ' '){TrailingPos -= 1;}
Size = ((TrailingPos+1)-LeadingPos);
for (int loops = 0; loops < Size; loops++)
{
if (MidCount > 0 && GameCommand[LeadingPos] == ' ')
{
LeadingPos += 1;
}
else
{
if (GameCommand[LeadingPos] == ' ')
{
MidCount += 1;
}
TempCommand += GameCommand[LeadingPos];
LeadingPos += 1;
}
}
GameCommand = TempCommand;
}
void Command::ReadCommand()
{
Trim();
string Parameter;
if (GameCommand.substr(0,3) == "go ")
{
Parameter = GameCommand.substr(3,string::npos);
CommandParameter.Go(Parameter);
}
else if (GameCommand.substr(0,4) == "dig ")
{
Parameter = GameCommand.substr(4,string::npos);
CommandParameter.Dig(Parameter);
}
else if (GameCommand.substr(0,4) == "eat ")
{
Parameter = GameCommand.substr(4,string::npos);
CommandParameter.Eat(Parameter);
}
else if (GameCommand.substr(0,4) == "exit" || GameCommand.substr(0,4) == "quit")
{
exit(0);
}
else if (GameCommand.substr(0,4) == "use ")
{
Parameter = GameCommand.substr(4,string::npos);
CommandParameter.Use(Parameter);
}
else if (GameCommand.substr(0,5) == "drop ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Drop(Parameter);
}
else if (GameCommand.substr(0,5) == "grab " || GameCommand.substr(0,5) == "take ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Pickup(Parameter);
}
else if (GameCommand.substr(0,5) == "help ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Help(Parameter);
}
else if (GameCommand.substr(0,5) == "look ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Look(Parameter);
}
else if (GameCommand.substr(0,5) == "sleep")
{
CommandParameter.Sleep();
}
else if (GameCommand.substr(0,6) == "check ")
{
Parameter = GameCommand.substr(6,string::npos);
CommandParameter.Check(Parameter);
}
else if (GameCommand.substr(0,6) == "climb ")
{
Parameter = GameCommand.substr(6,string::npos);
CommandParameter.Climb(Parameter);
}
else if (GameCommand.substr(0,6) == "throw ")
{
Parameter = GameCommand.substr(6,string::npos);
CommandParameter.Throw(Parameter);
}
else if (GameCommand.substr(0,7) == "attack ")
{
Parameter = GameCommand.substr(7,string::npos);
CommandParameter.Attack(Parameter);
}
else if (GameCommand.substr(0,7) == "search ")
{
Parameter = GameCommand.substr(7,string::npos);
CommandParameter.Search(Parameter);
}
else
{
cout << "Not a valid command.\n";
}
}
string Print()
{
return GameCommand;
}
The string GameCommand is what's not working.
class StartMap : public Map;
is syntactically incorrect. You need
class StartMap : public Map
{
// Details of class
} Start;
You need to make similar changes to JungleMap and RiverMap.
First thing I noticed was the semi-colon after each inheritance declaration..
class XXXXX : public Map; <-- that semi-colon shouldn't be there..
In the initialize function, I'm fairly certain you mean PlayerMap = new StartMap();
You'll need a destructor to delete it and a copy, move constructor as well as an assignment operator in order to assign, move or copy the class.
You can follow this to make the class conform to RAII: What is the copy-and-swap idiom?
There are lots of problems with your code layout.
This doesn't do anything:
//Player.h
#ifndef MAP_H
#define MAP_H
#endif
I guess you are trying to do an include guard. The proper layout is:
#ifndef PLAYER_H
#define PLAYER_H
// all your code for the header file goes here
class Player
{
// ....
};
#endif // no more code after this line
The next issue is that Player.h should include Map.h, not the other way around. Imagine you are the compiler. You are processing Player.h. You get down as far as Map *PlayerMap; . But you don't know what Map is because you haven't seen Map.h yet. So you have to give an error and stop compiling.
The map definitions in Map.h should look like:
class StartMap : public Map
{
//Code
};
The Start; you had on the end is poor style. It would cause undefined behaviour if two different .cpp files included Map.h because there would be two different global variables with the same name.
Moving onto the void Map::Initialize() function. You're supposed to use the constructor for initialization. Either way, my suggestion is that you don't implement this in Player.h. Instead, just have void Initialize();, and then in Map.cpp you could have:
// the global variables
StartMap start_map;
JungleMap jungle_map;
void Map::Initialize()
{
player_map = &start_map;
}
It's a good idea to use a different naming convention for classes than for variables. So that when someone sees StartMap for example, they know immediately whether it is a class name or a variable name.