Invalid Operands!!! Help ME!!!

I ve to add strings using + operator. I am using strcat function to add and I am getting following error
In function ‘int main()’:
cppfirst.cpp:44: warning: deprecated conversion from string constant to ‘char*’
cppfirst.cpp:44: warning: deprecated conversion from string constant to ‘char*’
cppfirst.cpp:48: error: invalid operands of types ‘char*’ and ‘char*’ to binary ‘operator+’



#include<iostream>
#include<cstring>
#include <string.h>
using namespace std;

class str{

//char *printstr,*prntstr;

public:
char *printstr,*prntstr;
str(char *str1,char *str2)
{
printstr=str1;
prntstr=str2;
}
char* getst1()
{
return printstr;
}
char* getst2()
{
return prntstr;
}

void show()
{
cout<<" "<<prntstr<<" "<<printstr<<endl;
}

friend char* operator+(str &op1);
};


char* operator+(str& op1)
{
char *temp;
temp=strcat(op1.getst1(),op1.getst2());
return temp;
}

int main()
{
str String("Welcome", "This is a String");
char *temp;
//temp=String.&printstr+String.&prntstr;
//temp=String.printstr+String.prntstr;
temp=String.getst1()+String.getst2();
String.show();
}
Try using proper, grown-up C++ strings. You can use these with the <string> header. The <string.h> header is a C header and does not give you proper C++ strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>

int main()
{
  std::string firstString("Beans ");
  std::string secondString("Eggs");

  std::string both = firstString + secondString;

  std::cout << both;
  return 0;
}
Last edited on
Ok i'll make the changes but what should i do of the operands if i cant use char*.

error: invalid operands of types ‘char*’ and ‘char*’ to binary ‘operator+’

Your function getst1 and getst2 return char*, so String.getst1()+String.getst2(); is an attempt to use the '+' operand with 2 char* objects. This cannot be done.

The '+' operand you defined works on two of your str class, so try using it on your str class rather than on two char* objects.

1
2
temp=String.getst1()+String.getst2();
temp=String+String;

This would use the '+' operator you defined.
Thanks i'll work on it..
Topic archived. No new replies allowed.