奇怪的c++类型不匹配

weird typing mismatch c++

本文关键字:类型 不匹配 c++      更新时间:2023-10-16

所以我不知道为什么我得到这个错误时,在.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();