Sponsored Content
Top Forums Programming C++ operator overloading error Post 303026988 by milhan on Thursday 6th of December 2018 02:10:16 PM
Old 12-06-2018
C++ operator overloading error

hi,
I defined my own String class and overloaded the output operator with
Code:
friend ostream& operator<<(ostream& os, const myString& str);
//overloads the << operator so it can be used to output values of type myString

which works fine. Until I try to execute the following statement:
Code:
// + concatenation operator is already defined for myString
cout << (str4=str2+str) << endl; //This statement concatenates str to str2, then assigns it to str4 and returns str4, right?

And, I get error
Code:
error: no match for ‘operator<<' (operand types are ‘std::ostream' {aka ‘std::basic_ostream<char>'} and ‘void')
  cout << (str4=str2+str) << endl;
  ~~~~~^~~~~~~~~~~~~~~~~~

The above code works fine when I change the concatenation statement as follows:
Code:
str4=str2+str;
cout << str4 << endl;

What did I miss?
 

10 More Discussions You Might Find Interesting

1. AIX

xlC compilation error when dealing with operator overloading

Hi, I have a piece of C++ code that can be compiled using g++, but reports an error when compiled with xlC: xlC -DHAVE_CONFIG_H -I../SRC -I../include -DNoChange -DSPRNG_MPI -q64 -DLONG64=long -I/usr/lpp/ppe.poe/include -DLONG64=long -c -o libsprng_a-bignum.o bignum.cpp "bignum.cpp",... (1 Reply)
Discussion started by: luop0812
1 Replies

2. UNIX for Dummies Questions & Answers

unary operator expected error

Hi I am doing a script like if then echo "table name dosent exist" exit fi the problem is if $table_name is null then i am getting the error Please help me Thanks in advance (2 Replies)
Discussion started by: ssuresh1999
2 Replies

3. Programming

C++ Optr Overloading

Hi All, In C++ one operator can be overloaded, but it can't be overloaded by it's own derieved class Please let me know operator. Thanks, Naga:cool: (1 Reply)
Discussion started by: Nagapandi
1 Replies

4. UNIX for Dummies Questions & Answers

File already exists error while using '>' operator

hi i am using the below code grep -v '^$' file1.lst >file1.lst but it gives file1.lst already exists. And i want to over rite on the same file Whats the work around? (5 Replies)
Discussion started by: jathin12
5 Replies

5. UNIX for Dummies Questions & Answers

Error : -ne: unary operator expected

find . -name "*.*"|xargs grep WT:DBF_WL>> $F Wfexist=`cat $F|grep $i` echo $Wfexist if ; then echo $Wfexist echo "Workflow Exist" else touch $O chmod 777 $O echo $Wfexist echo $WfExist >> $O fi I am getting the error that -ne: unary operator expected in the line with red... (2 Replies)
Discussion started by: ritu.s
2 Replies

6. Programming

Delete operator overloading with multiple arguments.

Hi, I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and deallocator like a pool, which makes me to overload the delete operator. Small example of the... (1 Reply)
Discussion started by: kapilkumawat
1 Replies

7. UNIX for Dummies Questions & Answers

[: =: unary operator expected error

Why am I getting this error.... #!/bin/sh # iOS-Ad-Remover # Marshall Ford @ marshallbford@gmail.com # This project is hosted @ http://ios-ad- # remover.sourceforge.net # Under the GNU GPL open source license clear echo if ; then echo "You need to be root to run this script."; exit 0; #... (24 Replies)
Discussion started by: mbf123
24 Replies

8. Programming

c++ assignment operator overloading

Hello everyone! Suppose that I have something like this A a; a.mem=new int; A b = a; where class A { public: int * mem; A() : mem(NULL) { } ~A() { if (mem!=NULL) delete mem; (1 Reply)
Discussion started by: bashuser2
1 Replies

9. UNIX for Dummies Questions & Answers

mysqld overloading cpu of VPS

Hi bros I have a VPS 512mb (Burst 2GB) with Kloxo installed and hosting few sites on it with not much traffic I am facing high cpu load for the last few days and seems mysqld is overloading the cpu Any suggestion will be appreciated Regards Rizwan Top output is as under top -... (2 Replies)
Discussion started by: rizwan65
2 Replies

10. Shell Programming and Scripting

Help - binary operator expected error

Hello Unix forum. I'm encountering the following error "binary operator expected error" and I cannot seem to solve the issue. I have the following source files to process: CPA_LOOKUP_dat.lst PROFILE_TXN__dat.lst TRANSACTION_CODE_dat.lst PROFILE_TXN_OUT_OF_BALANCE_dat.lst ... (2 Replies)
Discussion started by: pchang
2 Replies
binops(3bobcat) 						 Binary Operators						   binops(3bobcat)

NAME
binops - Template functions for class-type binary operators SYNOPSIS
#include <utility> #include <bobcat/typetrait> #include <bobcat/binops> DESCRIPTION
Classes can overload binary operators. A class named Class may overload these binary operators to suit its own needs, allowing, e.g., two Class type objects to be added after overloading operator+. Operators for the binary operators *, /, %, +, -, <<, >>, &, |, and ^ (in this man-page they are generically indicated as the `@' operator) can be overloaded by defining the operator@ function. If a class supports copy construction and if it offers binary assignment operators (i.e., it offers members of the form operator@=), then the matching binary operators can all be implemented identically. The move-aware Class &operator@(Class &&lhs, Class const &rhs) is easily implemented in terms of operator@= (note that the class itself doesn't have to be `move-aware' to define this function). The move-aware binary operator one requires a one line implementation, and as its implementation never changes it could safely be defined inline: Class operator@(Class &&lhs, Class const &rhs) { return std::move(lhs @= rhs); } The traditional binary operator can be implemented using its standard form: Class operator@(Class const &lhs, Class const &rhs) { Class tmp(lhs); tmp @= rhs; return tmp; } The implementation in bobcat/binops is slightly more complex as it allows from lhs or rhs promotions. As the binary operators can all be implemented alike their definitions are perfectly suited for templates: A class offering a particular operator@= then automatically also offers the matching binary operators after including bobcat/binops. Since the binary function templates are not instantiated until used their definitions can be processed by the compiler even if a class implements only a subset of the avail- able binary assignment operators. NAMESPACE
The binary operator functions templates in bobcat/binops are not implemented in a particular namespace. This allows sources to include bob- cat/binops in multiple namespaces. If bobcat/binops is to be used in multiple namespaces then the include safeguard (using the identifier INCLUDED_BOBCAT_BINOPS_) must be suppressed between inclusions of bobcat/binops in different namespaces. E.g., to make the binary operator function templates available in a source file using the namespace FBB and in a source file using the default namespace the following scheme can be used: #include <utility> // ensure std::move is available #include <bobcat/typetrait> // required by binops namespace MY_NAMESPACE { #include <bobcat/binops> // binary operators available in MY_NAMESPACE } #undef INCLUDED_BOBCAT_BINOPS_ // suppress the include guard #include <bobcat/binops> // read binops again so the binary // operators can be used in the // default namespace as well INHERITS FROM
- OVERLOADED OPERATORS
The function templates in bobcat/binops implement all arithmetic binary operators, both move-aware and the traditional binary operators, expecting constant lvalue references. They can be used if the matching binary assignment operators were implemented in the classes for which the templates must be instantiated. The following operators are available: Move-aware operators, using temporary objects for its left-hand side operands: o Class operator*(Class &&lhs, Class const &rhs): o Class operator/(Class &&lhs, Class const &rhs): o Class operator%(Class &&lhs, Class const &rhs): o Class operator+(Class &&lhs, Class const &rhs): o Class operator-(Class &&lhs, Class const &rhs): o Class operator<<(Class &&lhs, Class const &rhs): o Class operator>>(Class &&lhs, Class const &rhs): o Class operator&(Class &&lhs, Class const &rhs): o Class operator|(Class &&lhs, Class const &rhs): o Class operator^(Class &&lhs, Class const &rhs): `Traditional' operators, using lvalue references to constant objects for its left-hand side operands: o Class operator*(Class const &lhs, Class const &rhs): o Class operator/(Class const &lhs, Class const &rhs): o Class operator%(Class const &lhs, Class const &rhs): o Class operator+(Class const &lhs, Class const &rhs): o Class operator-(Class const &lhs, Class const &rhs): o Class operator<<(Class const &lhs, Class const &rhs): o Class operator>>(Class const &lhs, Class const &rhs): o Class operator&(Class const &lhs, Class const &rhs): o Class operator|(Class const &lhs, Class const &rhs): o Class operator^(Class const &lhs, Class const &rhs): The latter group of operators also support promotions. EXAMPLE
#include <iostream> #include <utility> #include "../../typetrait/typetrait" #include "../binops" class Demo { friend std::ostream &operator<<(std::ostream &out, Demo const &demo); int d_value; public: Demo(int value = 0) : d_value(value) {} Demo(Demo const &other) : d_value(other.d_value) { std::cout << "Demo CC called "; } Demo &operator+=(Demo const &rhs) { d_value += rhs.d_value; return *this; } }; std::ostream &operator<<(std::ostream &out, Demo const &demo) { return out << demo.d_value; } using namespace std; int main() { Demo four(4); Demo five(5); cout << four + five << ' ' << four + 5 << ' ' << 4 + five << ' '; } FILES
bobcat/binops - defines the binary operator function templates SEE ALSO
bobcat(7) BUGS
o The header files utility, defining std::move, and bobcat/typetrait are required by, but are not included by bobcat/binops. This was a design decision, see the NAMESPACE section. DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature; o bobcat_3.01.00-x.tar.gz: source archive; o bobcat_3.01.00-x_i386.changes: change log; o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location; BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'. COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL). AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl). libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 binops(3bobcat)
All times are GMT -4. The time now is 05:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy