gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:43:29 24 4
gpt4 key购买 nike

我遇到了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com