473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C++ operator overloading problem

1
I am trying out operator overloading. I have following code:

Expand|Select|Wrap|Line Numbers
  1. class Mystring{
  2.    public:
  3.       Mystring(char* ch)
  4.       {
  5.          data = new char[strlen(ch)*4];
  6.          strcpy(data,ch);
  7.       }
  8.       friend Mystring operator+(Mystring& s1, Mystring& s2);
  9.       friend ostream& operator<<(ostream& out, Mystring& s1);
  10.    char *data;
  11. };
  12. Mystring operator+( Mystring& s1, Mystring& s2)
  13. {
  14.    Mystring temp(s1.data);
  15.    strcat(temp.data,s2.data);
  16.    return temp;
  17. }
  18. ostream& operator<<(ostream& out, Mystring& s1)
  19. {
  20.    out<<s1.data;
  21.    return out;
  22. }
  23.  
In main I have defined two Mystring object.
Expand|Select|Wrap|Line Numbers
  1. Mystring s1("Abhilash");
  2. Mystring s2("Nair");
Following two statements work fine.
Expand|Select|Wrap|Line Numbers
  1. Mystring s3=s1+s2;
  2. cout<<s3<<endl;
But when I do
Expand|Select|Wrap|Line Numbers
  1. cout<<s1+s2<<endl;
I get compiler error. I am using cygwin compiler.

Below is the error I get. Why do you think it is happening?
May 15 '14 #1
1 1128
Banfa
9,065 Expert Mod 8TB
Actually giving the error you got would make this easier to diagnose however you could try

cout<<(s1+s2)<<endl;
May 15 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matti | last post by:
Hello, I have a problem comparing 2 values of type AnsiString using the == operator (C++ Builder 5). I use another class called TValue, which also defines the == operator. Now when I try to...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
6
by: titan0111 | last post by:
----inside of class called snowfall, i wrote---- int operator == (char); ----and right before main() i wrote---- int snowfall::operator == (char str) // comparing two member data...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
2
by: A_StClaire_ | last post by:
hopefully this code is not too obscure. lemme know and I can provide clarification. basically trying to implement a linked list (single links) manually. the function below is supposed to assign...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.