Hej. Problem polega na tym, ze do poki nie dodam do funkcji main elementu: "B = "Jest i czwarty.";", to wszystko ladnie dziala (zaznaczam ze wlasnie potrzebuje tego elementu :) ). Po dodaniu program przestaje dzialac, w ogole nic. Podejrzewam ze problem pochodzi od przeciazenia operatora <<, "String String::operator <<(char *str3)", poniewaz visual mi 'wykryl', ze cos sie psuje od elementu "string=new char[len];", wlasnie w tym przeciazeniu. Calkowicie nie mam pojecia co moze to powodowac.
#include <conio.h>
#include <iostream>
using namespace std;
class String
{
char *string;
int len;
public:
String(void)
{
len=1;
string=new char[len+1];
string[0]=0;
}
String(int n)
{
len = n;
string=new char[len+1];
string[0]=0;
}
String(char *s)
{
len = strlen(s);
string = new char[len+1];
strcpy(string,s);
}
String(String& str)
{
len=str.len;
string=new char[len+1];
strcpy(string,str.string);
}
void print(void)
{
cout << string ;
}
~String()
{
delete []string;
}
String& operator =(String &str);
String operator <<(char *str3);
friend String operator +(String str1,String str2);
};
String String::operator <<(char *str3)
{
String temp(len);
strcpy(temp.string,string);
delete []string;
len=len+strlen(str3)+1;
string=new char[len];
strcpy(string,temp.string);
strcat(string,str3);
return string;
}
String& String::operator =(String &str)
{
delete []string;
string=new char[str.len+1];
strcpy(string,str.string);
return *this;
}
String operator +(String str1,String str2)
{
String temp(str1.len+str2.len+1);
strcpy(temp.string,str1.string);
strcat(temp.string,str2.string);
return temp;
}
void main(void)
{
String A("Pierwszy element,"),B;
A << " drugi ";
B = A + "i trzeci. ";
B = "Jest i czwarty.";
B.print();
_getch();
return;
}