C++ 成员函数多态性问题

标签 c++ oop inheritance polymorphism

我遇到了 C++ 继承成员函数的问题,请看下面的代码:

binIO_t wtest(path, mode);
const void* Buff = "abcd";
wtest << Buff, 1; //no operator found
wtest.virtIO_t::operator<<(Buff), 1; //works fine

确切的编译器错误是:

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void *' (or there is no acceptable conversion) ...

我确实遗漏了一些微妙的减速,但我没能找到它。

我的 superclass.h 是:

#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

class virtIO_t{
private:
    virtIO_t();

public:
    virtIO_t(string filePath, string fileMode);
    ~virtIO_t();

    virtual virtIO_t& operator >> (void* Buf);
    virtual virtIO_t& operator << (const void* Buf);

    virtual virtIO_t& operator << (const char val) = 0;
    virtual virtIO_t& operator >> (char &val) = 0;
};

superclass.cpp 是:

#include "stdafx.h"
#include "virtIO_t.h"

virtIO_t::virtIO_t()
{
}

virtIO_t::~virtIO_t()
{
    ...
}

virtIO_t::virtIO_t(string filePath, string fileMode){
...
}


virtIO_t& virtIO_t::operator << (const void* Buff){
    ...
}
virtIO_t& virtIO_t::operator >> (void* Buff){
    ...
}

sub.h 是:

#pragma once
#include "virtIO_t.h"

class binIO_t : public virtIO_t{

public:
    binIO_t(string filePath, string mode);

    virtIO_t& operator << (const char val);
    virtIO_t& operator >> (char &val);

sub.cpp 是:

#include "stdafx.h"
#include "binIO_t.h"

binIO_t::binIO_t(string filePath, string mode) : virtIO_t(filePath, (string(mode) + "b").c_str()){}

    virtIO_t& binIO_t::operator << (const char val){
    ...
    }

    virtIO_t& binIO_t::operator >> (char &val){
    ...
    }

最佳答案

binIO_t声明它自己的 operator<<隐藏operator<<(void*)从基类。尝试 using virtIO_t::operator<<指令或明确定义 operator<<(void*)biunIO_t .

还有:, 1什么都不做。

关于C++ 成员函数多态性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193123/

相关文章:

python - 如何在 Python 中为特定输入类型强制执行特定输出类型?

c++ - 如何在 CMake 中导入库

c++ - 使用 Valgrind 调试程序 - 检测段错误

c# - 与 Funcs 的组合是否优于继承以更改单个函数中的行为?

php - 手动滚动 ORM 类——单例、静态父类?

德尔福企业版 : how can I apply the Visitor Pattern without circular references?

c++ - 从模板类的实例继承

java - Websphere 是用 Java 编写的吗?它在 JVM 中运行得这么快吗?

c++ - 相邻打印 2 个数组

C++ OpenSceneGraph 更改相机眼睛高度