Jump to content

operator+ c++


Recommended Posts

при компиляции появляется ошибка в строке st.str = st1.str + st2.str (error: invalid operands of types ‘char* const’ and ‘char* const’ to binary ‘operator+’) почему так происходит?

class String

{

private:

char* str;

int len;

static const int CINLIM = 80;

public:

String(const char* s);

String();

String(const String&);

~String();

int length() const {return len;}

String& operator=(const String&);

String& operator=(const char*);

char& operator[](int i);

const char& operator[](int i) const;

friend String operator+(const String& st1, const String& st2);

friend bool operator<(const String& st1, const String& st2);

friend bool operator>(const String& st1, const String& st2);

friend bool operator==(const String& st1, const String& st2);

friend std::ostream& operator<<(std::ostream& os, const String& st);

friend std::istream& operator>>(std::istream& is, String& st);

static int howmany();

String stringup();

void stringlow();

int has(char);

};

String operator+(const String& st1, const String& st2)

{

String st;

st.str = new char[strlen(st1.str) + strlen(st2.str) + 1];

st.str = st1.str + st2.str; //error: invalid operands of types ‘char* const’ and ‘char* const’ to binary ‘operator+

return st;

}

...

int main()

{

String s1("string1");

String s2 = "string2";

String s3;

s3 = s2 + s1;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...