1

I'm experiencing troubles with c++ inherrited member function, look at the following code:

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

Exact compiler error is:

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

I'm surly missing some subtle decleration yet I fail to find it.

my superclass.h is:

#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;
};

and the superclass.cpp is:

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

and the sub.h is:

#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);

and the sub.cpp is:

#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){
    ...
    }
GalB1t
  • 265
  • 1
  • 3
  • 13
  • 1
    What do you think those commas are doing? Probably not what you expect. – dwcanillas May 12 '15 at 14:01
  • @dwcanillas which commas are you referring to? – GalB1t May 12 '15 at 14:03
  • Nevermind the comma ... first, consider exactly what C++ is saying. *Is there* a version of `<<` which will accept a "pointer to `void`" as an acceptable input on its right-hand-side? Apparently there isn't. You're not coding what you think you're coding, and/or what you intend to be coding. Step back, take a deep breath, and look again. – Mike Robinson May 12 '15 at 14:05
  • @dwcanillas the operator<< is working fine when I call it explicitly, my question is why can't I use it implicitly? – GalB1t May 12 '15 at 14:05
  • @MikeRobinson what about the 'virtual virtIO_t& operator << (const void* Buf);' decleration? isn't it suffice? – GalB1t May 12 '15 at 14:07
  • 1
    Because the one being called implicitly is not the one being called explicitly. – Peter May 12 '15 at 14:08
  • @dwcanillas it's my fault that I havn't uploaded the entire class as you can read in the comments bellow, sorry for any incalrities and thank you for your effort. – GalB1t May 12 '15 at 14:21
  • 2
    @GalB1t http://stackoverflow.com/questions/5602112/when-to-overload-the-comma-operator – dwcanillas May 12 '15 at 14:21
  • @dwcanillas I accept that, yet it's an assignment of my c++ course... – GalB1t May 12 '15 at 14:23

1 Answers1

1

binIO_t declares it's own operator<< which hides operator<<(void*) from base class. Try using virtIO_t::operator<< directive or explicitly define operator<<(void*) in biunIO_t.

Also: , 1 does nothing.

Hcorg
  • 11,598
  • 3
  • 31
  • 36