怪异类型输入C++

怪异类型输入C++,c++,inheritance,C++,Inheritance,因此,我不确定在.h文件中定义格式为解析器*时为什么会出现此错误。编译器告诉我,在Parser::changeformatint方法中,我无法将int*转换为Parser*指针cpp文件尚未完成,因此如果其他方法抛出错误或看起来怪异,请忽略它们。为什么会发生这种情况,我如何修复它。这可能与继承有关,所以我将指出一个适合的解析器子类。另外,在任何情况下,如果你有关于其他部分的建议,那就请便 解析器.h #ifndef PARSER_H_ #define PARSER_H_ #include &l

因此,我不确定在.h文件中定义格式为解析器*时为什么会出现此错误。编译器告诉我,在Parser::changeformatint方法中,我无法将int*转换为Parser*指针cpp文件尚未完成,因此如果其他方法抛出错误或看起来怪异,请忽略它们。为什么会发生这种情况,我如何修复它。这可能与继承有关,所以我将指出一个适合的解析器子类。另外,在任何情况下,如果你有关于其他部分的建议,那就请便

解析器.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_ */

您正在尝试实例化从解析器基类派生的xmlpar实例,但不在Parser.cpp中包含派生类xmlpar的头。编译器在此处找不到xmlpar的定义:


我也怀疑这是否是一个好的做法。

Parser.cpp似乎不包括xmlpar.h,也不了解xmlpar的类型……既然有很多已经调试过的免费库,为什么还要编写XML解析器呢。如果您准备了一个,您可能会发现问题。顺便说一句,请避免使用此->语法,因为它不是必需的。更改参数或成员变量的名称。将常量添加到不打算更改的参数中,例如打开方法中的文件名。
#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
 *
 *  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
 *
 *  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);
}
this->format = new xmlpar();