c++ member function polymorphism issue - c++

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

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.

Related

Unable to find segmentation fault error in c++

I have 3 c++ files, instrument.h, percussion.h, and instrumentApp.cpp. Instrument.h is the base class and percussion.h inherits it. Percussion objects are defined and implemented in the instrumentApp.cpp class. Whenever I run instrumentApp.cpp, I get the segmentation fault error.
I have managed to trace the cause of the error to the overloaded << operator function in percussion.h where I am calling a method of the base class instrument.h. For some reason, my code is unable to call methods of the base class and I don't know why. Can you please help me?
Here is the instrument.h class
#ifndef INSTRUMENT_H
#define INSTRUMENT_H
class Instrument{
private:
std::string name;
std::string sound;
std::string lowRange;
std::string highRange;
public:
Instrument(std::string name, std::string sound, std::string lowRange, std::string highRange){
this->name = name;
this->sound = sound;
this->lowRange = lowRange;
this->highRange = highRange;
}
std::string getName() const{
return this->name;
}
std::string play()const {
return this->sound;
}
std::string getLowRange() const{
return this->lowRange;
}
std::string getHighRange() const{
return this->highRange;
}
bool isWind();
bool isWoodWind();
bool isBrass();
bool isKeyboard();
bool isPercussion();
bool isStrings();
friend std::ostream &operator <<(std::ostream &os, const Instrument &instrument){
}
};
#endif
Here is the percussion.h class
#ifndef PERCUSSION_H
#define PERCUSSION_H
#include "instrument.h"
class Percussion : public Instrument{
private:
bool struck;
public:
Percussion(std::string name, std::string sound, std::string lowRange, std::string highRange, bool struck) : Instrument(name,sound,lowRange,highRange){
this->struck=struck;
}
bool isStrucked() const {
return this->struck;
}
bool isPercussion() {
return true;
}
std::string getType() const{
if(this->struck){
return "struck";
}
else{
return "";
}
}
friend std::ostream &operator <<(std::ostream &os, Percussion &percussion){
//The error stems from this line of code
//Apparently, the getName() method in the base class isn't called
os<<percussion.getName();
}
};
#endif
Here is the implementation file instrumentApp.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include "instrument.h"
#include "percussion.h"
#include "strings.h"
using namespace std;
int main() {
Percussion timpani("timpani", "boom", "D2", "A2", true);
cout << timpani << endl;
Percussion harp("harp", "pling", "Cb1", "F#7", false);
cout << harp << endl;
return 0;
}
The problem here is that I wasn't returning the os object when I overloaded the << operator.
The fix is as follows in the percussion.h file
friend std::ostream &operator <<(std::ostream &os, Percussion &percussion){
os<<percussion.getName();
return os;
}

C++ output operator overloading

I wanted to try to overload "<<" operator for an output stream and a custom class Article.
my.h
#include <iostream>
using namespace std;
class Article {
string body;
string header;
string author;
public:
Article();
void addHeader(const string&);
void addBody(const string&);
string getHeader();
string getBody();
};
my.cpp
string Article::getHeader() {
return header;
}
void Article::addBody(const string& body) {
this->body = body;
}
void Article::addHeader(const string& header) {
this->header = header;
}
ostream& operator<<(ostream& os, Article& article) {
return os << article.getHeader() << "\n\n" << article.getBody();
}
main.cpp
#include <iostream>
#include <string>
#include "my.h"
void main() {
char bye;
Article article = Article();
article.addBody("This is my article! thank you!");
article.addHeader("Header");
cout << article.getHeader() << "\n";
cout << article.getBody() << "\n";
cout << article;
cin >> bye;
}
This code doesn't compile. VS 2013 says:
binary '<<' : no operator found which takes a right-hand operand of type 'Article' (or there is no acceptable conversion)
If I remove the last line, it compiles successfully and the output of getHeader() and getBody() is as expected. They both return strings so it should be a piece of cake.
This problem seems very simple, however, as simple as it is I can't figure out what is happenning.
Now that you have posted real code the answer is obvious.
You define your operator << in my.cpp, but you don't declare it in my.h, so when compiling main.cpp the compiler has no way of knowing it exists.
Add a declaration of the function to your header.

ofstream writes hex instead of char*

I try to write a simple Logger but the ofstream writes hex instead of the characters to the file.
Definition:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#define LOG Logger::getInstance()
class Logger
{
public:
static Logger m_instance;
static Logger &getInstance()
{
if (m_logfile == nullptr)
{
m_logfile = new std::ofstream(m_logfile_string.c_str(),
std::ofstream::out | std::ofstream::app);
}
return m_instance;
}
Logger &operator<<(const std::string &c);
Logger &operator<<(const char c[]);
private:
static std::ofstream *m_logfile;
static std::string m_logfile_string;
Logger() {};
Logger(const Logger &) = delete;
Logger(Logger &&other) = delete;
Logger &operator=(const Logger &other) = delete;
Logger &operator=(Logger &&other) = delete;
~Logger()
{
if (m_logfile != nullptr)
m_logfile->close();
};
std::string currentDateTime() const;
};
Impl.
#include "Logger.h"
#include <iostream>
#include <ctime>
Logger Logger::m_instance;
std::ofstream *Logger::m_logfile = nullptr;
std::string Logger::m_logfile_string = "log.txt";
Logger &Logger::operator<<(const std::string &c)
{
this->operator<<(c.c_str());
return *this;
}
Logger &Logger::operator<<(const char c[])
{
std::cout << currentDateTime() << " - "
<< c << std::endl;
m_logfile->operator<<(c);
return *this;
}
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
std::string Logger::currentDateTime() const
{
auto now = time(nullptr);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
Usage:
#include "Logger.h"
void main()
{
LOG << "started"; // Logger::getInstance() << "started";
}
Outcome:
00007FF696EF3C00 in the log.txt The console output is right.
Whats going wrong here?
You use member function of iostream:
m_logfile->operator<<(c);
There is no member function operator<<(char* c), but there is operator<<(void* p), so pointer is implicitly converted. See documentation on ostream.
Operators for char* are not class member functions:
ostream& operator<< (ostream& os, const char* s);
See here: operator<< (ostream)
So you need this code:
operator<<(*m_logfile, c);
Or much more cleaner:
(*m_logfile) << c;
Test example:
#include <iostream>
int main() {
std::cout.operator<<("test");
operator<<(std::cout, "test");
std::cout << "test";
}
Prints 0x400944testtest.
If you use
(*m_logfile)<<c;
instead of
m_logfile->operator<<(c);
It will work.
Reason:
Your syntax calls the member function ostream::operator<<() which is not defined for char* nor for string. It's only defined for void* which always displays address in hex.
The classic syntax calls here the non member function ostream std::operator<<(...) which has overloads for string and char*

C++ ostringstream strange behavior when chaining in cout is used

I am a C++ beginner ( came from Java ). I have the following code:
//#include <boost/algorithm/string.hpp>
#include <iostream>
#include <math.h>
#include <vector>
#include <string.h>
#include <string>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <memory>
#include <assert.h>
#include <cctype>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(int x,int y){
this->x=x;
this->y=y;
}
int getX(){
return x;
}
int getY(){
return y;
}
operator const char*(){
return toString().c_str();
}
string toString(){
ostringstream stream;
stream<<"( "<<x<<", "<<y<<" )";
return stream.str();
}
};
class Line{
private:
Point p1=Point(0,0);
Point p2=Point(0,0);
public:
Line(Point p1, Point p2){
this->p1=p1;
this->p2=p2;
}
Point getP1(){
return p1;
}
Point getP2(){
return p2;
}
operator const char*(){
ostringstream stream;
stream<<"[ "<<p1<<" -> "<<p2<<" ]";
return stream.str().c_str();
}
// operator const char*(){
// ostringstream stream;
// stream<<"[ "<<p1<<" -> ";
// stream<<p2<<" ]";
// return stream.str().c_str();
// }
};
int main()
{
Line line=Line(Point(1,2), Point(3,4));
cout<<line<<endl;
cout<<"\nProgram exited successfully."<<endl;
return 0;
}
I have redefined the operator const* so that I can use cout<
But, If I run the program as it is now, with the second block commented out ( I have 2 versions of operator const*, and by default the second one is commented out ) ,it will display
[ (1, 2) -> (1, 2) ]
But when running with the second block uncommented, the output is as expected:
[ (1, 2) -> (3, 4) ]
The issue seems to occur when I display both Point objects in the same line ( some kind of chaining, though I don't know if chaining is the right word here )
My question, is,why is this happening?
UPDATE
I have added the std::ostream& operator << function to my Line class but now I'm receiving the following errors:
/home/ryu/qt_workspace/hello/main.cpp:67: error: 'std::ostream& Line::operator<<(std::ostream&, const Line&)' must take exactly one argument
/home/ryu/qt_workspace/hello/main.cpp:77: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
Regards,
Aurelian
If you want to use cout <<, there is a more direct way to do that.
Add this function to Line.
friend std::ostream& operator << ( std::ostream & os, const Line & l ){
os << "[ " << l.p1 << " -> " << l.p2 << " ]";
return os;
}
You should also note that your approach was returning invalid memory - this is a significant way that Java differs from C++.
return stream.str().c_str(); // Danger!
stream was declared in operator const char*() which limits its lifetime to that function. It is destroyed when that scope is exited. As a result, you are returning a pointer to something that no longer exists.
actually I think with C++11 returning the string by value is perfectly fine, so you can do the transfer there instead of using the cstring underneath.
What are move semantics?

compile libfacebookcpp on linux , 'StringBuilder' was not declared

when compile, it occurs an error:
PagingInfo.hpp:35: error: ‘StringBuilder’ was not declared in this scope.
I have inlude the right head file, but why compiler can not find the difinition of StringBuilder?
Utils.hpp:
#ifndef LIBFACEBOOKCPP_UTILS_H_
#define LIBFACEBOOKCPP_UTILS_H_
template<class TData, class TStr>
inline TData fromString(const TStr &str)
{
std::stringstream oss;
oss << str;
TData t;
oss >> t;
return t;
}
class StringBuilder
{
public:
inline operator const std::string () const
{
return oss.str();
}
private:
std::ostringstream oss;
};
#endif // LIBFACEBOOKCPP_UTILS_H_
PagingInfo.hpp
#ifndef LIBFACEBOOKCPP_PAGING_INFO_H_
#define LIBFACEBOOKCPP_PAGING_INFO_H_
#include "Utils.hpp"
namespace LibFacebookCpp
{
struct PagingInfo
{
PagingInfo(unsigned int offset_, unsigned int limit_) : offset(offset_), limit(limit_) { }
bool IsValid() const { return 0 != limit; }
void GetUri(Uri *uri) const
{
LIBFACEBOOKCPP_ASSERT(uri);
uri->query_params["limit"] = StringBuilder() << offset;
uri->query_params["offset"] = StringBuilder() << limit;
}
...
};
} // namespace LibFacebookCpp
#endif // LIBFACEBOOKCPP_PAGING_INFO_H_
When I add enough skeleton code to get this down to just your issue in ideone, I get a different error:
prog.cpp: error: no match for 'operator<<' in 'StringBuilder() << ((const LibFacebookCpp::PagingInfo*)this)->LibFacebookCpp::PagingInfo::offset'
Your StringBuilder class does not have a << operator defined. In order to use:
StringBuilder() << offset;
You will need to define one.
Between you and me, there are about 15 overloads of that operator for stringstreams (one for every primitive type). It would be a massive waste of time to reimplement all of them. Just use a stringstream.