I was trying to implement operator overloading in c-string in the following code

#include <cstring>
#include <iostream>

using namespace std;

class string
{
      char *st;
      int len;
public:
       string():st(NULL), len(0) {}
       string(const char *s);
       string(const string & s);
       ~string();
       friend string operator+(const string &s1, const string & s2);
       friend int operator<=(const string &s1, const string &s2);
       void show()
       {
            cout<<st<<endl;
       }      
};

string::string(const char *s)
{
      len = strlen(s);
      st = new char[len+1];
      strcpy(st,s);
}

string::string(const string &s)
{
     len = strlen(s);
     st = new char[len+1];
     strcpy(st,s);
}

string::~string()
{
     delete st;
}

string operator+(const string &s1, const string &s2)
{
       string ts;
       ts.len = s1.len + s2.len;
       ts.st = new char[ts.len+1];
       strcpy(ts.st, s1);
       strcat(ts.st, s2);
       return ts;
}

string operator<=(const string &s1, const string &s2)
{
       return strcmp(s1.st, s2.st);
}

int main()
{
    string s1 = "new";
    string s2 = "york";
    string s3 = "delhi";
    char ch;
    cin>>ch;
    return 0;
}

can anyone tell me the bug in this code.

the error is...

Compiler: Default compiler
Executing g++.exe...
g++.exe "F:\Dev-Cpp\string.cpp" -o "F:\Dev-Cpp\string.exe" -I"F:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"F:\Dev-Cpp\include\c++\3.4.2\backward" -I"F:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"F:\Dev-Cpp\include\c++\3.4.2" -I"F:\Dev-Cpp\include" -L"F:\Dev-Cpp\lib"
F:\Dev-Cpp\string.cpp:23: error: `string' has not been declared
F:\Dev-Cpp\string.cpp:24: error: ISO C++ forbids declaration of `string' with no type
F:\Dev-Cpp\string.cpp: In function `int string(const char*)':
F:\Dev-Cpp\string.cpp:25: error: `len' undeclared (first use this function)
F:\Dev-Cpp\string.cpp:25: error: (Each undeclared identifier is reported only once for each function it appears in.)
F:\Dev-Cpp\string.cpp:26: error: `st' undeclared (first use this function)
F:\Dev-Cpp\string.cpp: At global scope:
F:\Dev-Cpp\string.cpp:30: error: `string' has not been declared

F:\Dev-Cpp\string.cpp:30: error: expected `,' or `...' before '&' token
F:\Dev-Cpp\string.cpp:31: error: ISO C++ forbids declaration of `string' with no type

F:\Dev-Cpp\string.cpp:31: error: ISO C++ forbids declaration of `string' with no type

F:\Dev-Cpp\string.cpp: In function `int string(int)':
F:\Dev-Cpp\string.cpp:32: error: `len' undeclared (first use this function)
F:\Dev-Cpp\string.cpp:32: error: `s' undeclared (first use this function)
F:\Dev-Cpp\string.cpp:33: error: `st' undeclared (first use this function)
F:\Dev-Cpp\string.cpp: At global scope:
F:\Dev-Cpp\string.cpp:37: error: expected constructor, destructor, or type conversion before '::' token
F:\Dev-Cpp\string.cpp:37: error: expected `,' or `;' before '::' token
F:\Dev-Cpp\string.cpp:42: error: expected constructor, destructor, or type conversion before "operator"
F:\Dev-Cpp\string.cpp:42: error: expected `,' or `;' before "operator"
F:\Dev-Cpp\string.cpp:52: error: expected constructor, destructor, or type conversion before "operator"
F:\Dev-Cpp\string.cpp:52: error: expected `,' or `;' before "operator"
F:\Dev-Cpp\string.cpp: In function `int main()':
F:\Dev-Cpp\string.cpp:59: error: `string' undeclared (first use this function)
F:\Dev-Cpp\string.cpp:59: error: expected `;' before "s1"
F:\Dev-Cpp\string.cpp:60: error: expected `;' before "s2"
F:\Dev-Cpp\string.cpp:61: error: expected `;' before "s3"

Execution terminated

Since string is in the std namespace don't grap everything:

using namespace std;

Be specific:

using std::cout;
using std::cin;
using std::endl;

Or choose a new class name different from string .

Then you'll be left "more normal" errors to fix.

You might want to change your destructor.

delete [] st;

instead of

delete st;

You'll be creating memory leaks if you don't.

You might want to change your destructor.

delete [] st;

instead of

delete st;

You'll be creating memory leaks if you don't.

Not memory leak, but undefined behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.