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

时间:2015-05-12 13:58:33

标签: 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

确切的编译错误是:

  

错误1错误C2679:二进制'&lt;&lt;' :没有找到哪个运算符采用'void *'类型的右手操作数(或者没有可接受的转换)......

我很遗憾地发现了一些微妙的贬低,但我找不到它。

我的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){
    ...
    }

1 个答案:

答案 0 :(得分:1)

operator<<声明它自己的operator<<(void*)隐藏了基类的using virtIO_t::operator<<。尝试使用operator<<(void*)指令或在biunIO_t中明确定义, 1

另外:{{1}}什么都不做。