#include <iostream>
#include <string>
using namespace std;
class String {
public:
String();
String(const char []);
String(const String &);
int Compare(const String &) const;
int Compare(const char[]) const;
String & Copy(const String &);
String & Copy(const char[]);
size_t Len() const;
String & Conc(const char[]);
String & Conc(const String &);
String Display() const;
private:
size_t Letters;
size_t Slots;
char* Fazah;
};
String::String() {
Letters = 0;
Slots = Letters;
Fazah = new char [Slots + 1];
Fazah[0]= '\0';
}
String::String(const char otherVar[]) {
Letters = strlen(otherVar);
Slots = Letters;
Fazah = new char [Slots + 1];
strcpy(Fazah, otherVar);
}
String::String(const String & otherVar) {
Slots = otherVar.Slots;
Letters = otherVar.Letters;
Fazah = new char [Slots + 1];
strcpy (Fazah, otherVar.Fazah);
cout <<"Copy const"<< endl;
}
int String::Compare (const String & otherVar) const {
return strcmp (Fazah, otherVar.Fazah);
}
int String::Compare(const char otherVar []) const {
return strcmp (Fazah, otherVar);
}
inline size_t String::Len()const {
return Letters;
}
String String::Display() const {
return* this;
}
String & String::Copy(const String & otherVar) {
delete[] Fazah;
Letters = otherVar.Letters;
Slots = otherVar.Letters;
Fazah = new char [Slots + 1];
return *this;
}
String & String::Copy(const char otherVar []) {
delete[] Fazah;
Letters = strlen (otherVar);
Slots = Letters;
Fazah = new char [Slots + 1];
return *this;;
}
String & String::Conc(const String & otherVar) {
//delete[] Fazah;
Letters = Letters + otherVar.Letters;
Slots = Slots + otherVar.Slots;
Fazah = new char [Slots + 1 ];
return*this;
}
String & String::Conc(const char otherVal[]) {
Slots = Slots + Letters;
Letters = Letters + strlen(otherVal);
Fazah = new char [Slots + 1];
return* this;
}
int main() {
String Str2("abcdefg");
String Len(Str2);
}
Я изучаю C++ в течение нескольких недель, поэтому я все еще относительно новичок в этом. там были такие моменты, когда я просто не уверен, что искать, чтобы исправить мою проблему, это один из тех случаев. Это даже не синтаксическая ошибка, поэтому сложнее ее исправить. Возможно, это связано с тем, что я по-прежнему новичок в этом; в моей голове String Len(Str2)
должен возвращать длину строки, но это не только возвращает cout <<"Copy const"<< endl
. Не совсем уверен, что делать здесь.Почему моя функция длины строки не возвращает длину?
Все, что утверждение не является создать вторую переменную с именем 'Len' , он вообще не вызывает метод «Лен». –
'#include' is for 'std :: string', возможно, вы имели в виду' #include 'непосредственно для функций строки C? Кроме того, зачем использовать 'std', если у вас нет кода, использующего библиотеку' std' (а также не должно быть этого в include-файле, если это где этот код)? –
crashmstr