Compile Errors, Please Help
Code:
Hello,
I am doing an exercise in my book and im suing the answer codes provided, and it still does not work, here is the code:
#include <stdafx.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class String {
private:
char *ptr;
public:
String();
String(char *s);
String(const String &src);
~String();
String& operator=(const String &src) {cpy(src.ptr); return *this;}
String& operator=(char *s) {cpy(s); return *this;}
String operator+(char *s);
int operator==(const String &other);
operator char*() {return ptr;}
operator int() {return atoi(ptr);} //this is the convert to int function
void cat(char *s);
void cpy(char *s);
};
int main() {
String a, b, c;
a = "I ";
b = "am ";
c = "so ";
String d = a + b + c + "very happy!\n";
cout << d;
return 0;
}
//declaring String class member functions
String::String() {
ptr = new char[1];
ptr[0] = '\0';
}
String::String(char *s) {
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
String::String(const String &src) {
int n = strlen(src.ptr);
ptr = new char[n + 1];
strcpy(ptr, src.ptr);
}
String::~String() {
delete [] ptr;
}
int String::operator==(const String &other) {
return (strcmp(ptr, other.ptr) == 0);
}
String String::operator+(char *s) {
String new_str(ptr);
new_str.cat(s);
return new_str;
}
//cpy- copy string function
void String::cpy(char *s) {
delete [] ptr;
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
//cat- concatenation string function
void String::cat(char *s) {
int n = strlen(ptr) + strlen(s);
char *p1 = new char[n + 1];
strcpy(p1, ptr);
strcat(p1, s);
delete [] ptr;
ptr = p1;
}
okay, so the exercies was to add a convert to int function which is marked above, the code compiled fine before i added it, but now i add it and it does not, please help.