奇怪的c++类型不匹配
weird typing mismatch c++
所以我不知道为什么我得到这个错误时,在.h文件它被定义为格式是一个解析器*。编译器告诉我,在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+" openedn";
return true;
}
else
{
std::cout<<"Error: "+filename+" unable to be openedn";
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);
}
您正在尝试实例化xmlpar
的实例,该实例派生自Parser
基类,而不包括Parser.cpp
派生类xmlpar
的头。编译器在这里没有找到xmlpar
的定义:
this->format = new xmlpar();
相关文章:
- 将 unordered_map 与 Catch2 谓词一起使用时类型不匹配
- 重载函数的地址与所需类型不匹配
- 参数错误可能与类型不匹配有关?
- 调用子例程时类型不匹配
- POSIX(C )参数类型不匹配
- typedef 类型不匹配,将其传递到函数中
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 候选模板被忽略:推断的类型与调整后的类型不匹配
- 内联 ASM:'out'的操作数类型不匹配
- 为什么我们有一个类型不匹配
- C++类和freeglut(参数类型不匹配)
- 警告 639:二进制操作中类型 'uint64' 的强类型不匹配
- 警告 634:相等或有条件的强类型不匹配(类型"bool")
- 不了解C++类型不匹配:const Foo* to Foo* const&
- 函数声明和定义的返回类型不匹配,编译器可以吗?
- 类型不匹配
- 返回指向常量getter的指针时,返回值类型与函数类型不匹配
- llvm pass replaceAllUses与类型不匹配?
- 参数类型不匹配(无效*)
- 重载运算符中的类型不匹配(写入管道)