所以我不确定为什么我在.h文件中得到这个错误时,它被定义为格式是Parser *。编译器告诉我在方法Parser :: changeformat(int)中我无法将int *转换为Parser *指针(cpp文件尚未完成,所以如果它们抛出错误或看起来很奇怪,请忽略所有其他方法) 。为什么会发生这种情况,我该如何解决它。这可能与继承有关,所以我将指出一个适合的解析器的子类。此外,无论如何,如果您对其他部分有任何建议,请成为我的客人。
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);
}