我在Oj上做一道构造string类的题目,按照题目要求,我在VS上补全了代码,自己运行也没问题,贴到OJ上却直接编译错误,
题目:OpenJudge - 019:全面的MyString
我的代码:
链接:https://pan.baidu.com/s/1VlEc0saMWAn-aAsNVq0AfQ
提取码:76z5
Oj 的报错:
/home/runner/temp/29808948.216758/Main.cc: In function ‘int main()’:/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’ MyString(MyString& mstr_){ ^/home/runner/temp/29808948.216758/Main.cc:36:2: note: after user-defined conversion: MyString::MyString(char*) MyString(char* str_ = "") { ^/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’ MyString(MyString& mstr_){ ^/home/runner/temp/29808948.216758/Main.cc:36:2: note: after user-defined conversion: MyString::MyString(char*) MyString(char* str_ = "") { ^/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’ MyString(MyString& mstr_){ ^/home/runner/temp/29808948.216758/Main.cc:36:2: note: after user-defined conversion: MyString::MyString(char*) MyString(char* str_ = "") { ^/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’
VS上的运行结果:
代码:
#include <cstdlib>
#include <iostream>
using namespace std;
//int strlen(const char* s)
//{
//int i = 0;
//for (; s[i]; ++i);
//return i;
//}
//void strcpy(char* d, const char* s)
//{
//int i = 0;
//for (i = 0; s[i]; ++i)
//d[i] = s[i];
//d[i] = 0;
//}
int strcmp(const char* s1, const char* s2)
{
for (int i = 0; s1[i] && s2[i]; ++i) {
if (s1[i] < s2[i])
return -1;
else if (s1[i] > s2[i])
return 1;
}
return 0;
}
//void strcat(char* d, const char* s)
//{
//int len = strlen(d);
//strcpy(d + len, s);
//}
class MyString
{
// 在此处补充你的代码
char string[30];
public:
MyString(char* str_ = "") {
strcpy(string, str_);
//cout << "使用构造函数构造了" << string << endl;
}
MyString(MyString& mstr_){
strcpy(string, mstr_.string);
//cout << "使用复制构造函数构造了" << string << endl;
}
~MyString() {
//cout << string << "已经析构" << endl;
}
MyString& operator=(MyString& mstr_) {
strcpy(string, mstr_.string);
return *this;
}
MyString& operator=(char* str_) {
strcpy(string, str_);
return *this;
}
MyString operator+(MyString& mstr_) {
MyString temp(string);
strcat(temp.string, mstr_.string);
return temp;
}
MyString operator+(char* str_) {
MyString temp(string);
strcat(temp.string, str_);
return temp;
}
MyString& operator+=(char* str_) {
strcat(string, str_);
return *this;
}
char & operator[](int ud_) {
return string[ud_];
}
MyString operator()(int start_, int len_) {
MyString temp;
int i = 0;
for (; i < len_; ++i) {
temp.string[start_ + i] = string[start_ + i];
}
temp.string[start_ + i] = 0;
return temp;
}
bool operator<(MyString& mstr_) {
if (strcmp(string, mstr_.string) == -1) {
return true;
}
else {
return false;
}
}
bool operator>(MyString& mstr_) {
if (strcmp(string, mstr_.string) == 1) {
return true;
}
else {
return false;
}
}
bool operator==(MyString& mstr_) {
if (strcmp(string, mstr_.string) == 0) {
return true;
}
else {
return false;
}
}
friend ostream& operator<<(ostream& cout_, MyString& mstr_) {
cout_ << mstr_.string;
return cout_;
}
friend MyString operator+(char* str_, MyString& mstr_) {
char temp[30];
strcpy(temp, str_);
strcat(temp, mstr_.string);
MyString tempp(temp);
return tempp;
}
};
int CompareString(const void* e1, const void* e2)
{
MyString* s1 = (MyString*)e1;
MyString* s2 = (MyString*)e2;
if (*s1 < *s2)//operator< == >
return -1;
else if (*s1 == *s2)
return 0;
else if (*s1 > *s2)
return 1;
}
int main()
{
MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);//默认构造函数,字符串构造函数,复制构造函数,析构函数
MyString SArray[4] = { "big","me","about","take" };
cout << "1. " << s1 << s2 << s3 << s4 << endl;//operator<<
s4 = s3;//operator=
s3 = s1 + s3;//operator+
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;//operator[]
s2 = s1;
s1 = "ijkl-";//operator=
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";//operator+=
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;//operator+ 全局
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";//operator+
cout << "11. " << s1 << endl;
qsort(SArray, 4, sizeof(MyString), CompareString);
for (int i = 0; i < 4; i++)
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0, 4) << endl;//operator()
//s1的从下标5开始长度为10的子串
cout << s1(5, 10) << endl;
return 0;
}
题目:OpenJudge - 019:全面的MyString
我的代码:
链接:https://pan.baidu.com/s/1VlEc0saMWAn-aAsNVq0AfQ
提取码:76z5
Oj 的报错:
/home/runner/temp/29808948.216758/Main.cc: In function ‘int main()’:/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’ MyString(MyString& mstr_){ ^/home/runner/temp/29808948.216758/Main.cc:36:2: note: after user-defined conversion: MyString::MyString(char*) MyString(char* str_ = "") { ^/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’ MyString(MyString& mstr_){ ^/home/runner/temp/29808948.216758/Main.cc:36:2: note: after user-defined conversion: MyString::MyString(char*) MyString(char* str_ = "") { ^/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’ MyString(MyString& mstr_){ ^/home/runner/temp/29808948.216758/Main.cc:36:2: note: after user-defined conversion: MyString::MyString(char*) MyString(char* str_ = "") { ^/home/runner/temp/29808948.216758/Main.cc:133:49: error: invalid initialization of non-const reference of type ‘MyString&’ from an rvalue of type ‘MyString’ MyString SArray[4] = {"big","me","about","take"}; ^/home/runner/temp/29808948.216758/Main.cc:40:2: note: initializing argument 1 of ‘MyString::MyString(MyString&)’
VS上的运行结果:
代码:
#include <cstdlib>
#include <iostream>
using namespace std;
//int strlen(const char* s)
//{
//int i = 0;
//for (; s[i]; ++i);
//return i;
//}
//void strcpy(char* d, const char* s)
//{
//int i = 0;
//for (i = 0; s[i]; ++i)
//d[i] = s[i];
//d[i] = 0;
//}
int strcmp(const char* s1, const char* s2)
{
for (int i = 0; s1[i] && s2[i]; ++i) {
if (s1[i] < s2[i])
return -1;
else if (s1[i] > s2[i])
return 1;
}
return 0;
}
//void strcat(char* d, const char* s)
//{
//int len = strlen(d);
//strcpy(d + len, s);
//}
class MyString
{
// 在此处补充你的代码
char string[30];
public:
MyString(char* str_ = "") {
strcpy(string, str_);
//cout << "使用构造函数构造了" << string << endl;
}
MyString(MyString& mstr_){
strcpy(string, mstr_.string);
//cout << "使用复制构造函数构造了" << string << endl;
}
~MyString() {
//cout << string << "已经析构" << endl;
}
MyString& operator=(MyString& mstr_) {
strcpy(string, mstr_.string);
return *this;
}
MyString& operator=(char* str_) {
strcpy(string, str_);
return *this;
}
MyString operator+(MyString& mstr_) {
MyString temp(string);
strcat(temp.string, mstr_.string);
return temp;
}
MyString operator+(char* str_) {
MyString temp(string);
strcat(temp.string, str_);
return temp;
}
MyString& operator+=(char* str_) {
strcat(string, str_);
return *this;
}
char & operator[](int ud_) {
return string[ud_];
}
MyString operator()(int start_, int len_) {
MyString temp;
int i = 0;
for (; i < len_; ++i) {
temp.string[start_ + i] = string[start_ + i];
}
temp.string[start_ + i] = 0;
return temp;
}
bool operator<(MyString& mstr_) {
if (strcmp(string, mstr_.string) == -1) {
return true;
}
else {
return false;
}
}
bool operator>(MyString& mstr_) {
if (strcmp(string, mstr_.string) == 1) {
return true;
}
else {
return false;
}
}
bool operator==(MyString& mstr_) {
if (strcmp(string, mstr_.string) == 0) {
return true;
}
else {
return false;
}
}
friend ostream& operator<<(ostream& cout_, MyString& mstr_) {
cout_ << mstr_.string;
return cout_;
}
friend MyString operator+(char* str_, MyString& mstr_) {
char temp[30];
strcpy(temp, str_);
strcat(temp, mstr_.string);
MyString tempp(temp);
return tempp;
}
};
int CompareString(const void* e1, const void* e2)
{
MyString* s1 = (MyString*)e1;
MyString* s2 = (MyString*)e2;
if (*s1 < *s2)//operator< == >
return -1;
else if (*s1 == *s2)
return 0;
else if (*s1 > *s2)
return 1;
}
int main()
{
MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);//默认构造函数,字符串构造函数,复制构造函数,析构函数
MyString SArray[4] = { "big","me","about","take" };
cout << "1. " << s1 << s2 << s3 << s4 << endl;//operator<<
s4 = s3;//operator=
s3 = s1 + s3;//operator+
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;//operator[]
s2 = s1;
s1 = "ijkl-";//operator=
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";//operator+=
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;//operator+ 全局
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";//operator+
cout << "11. " << s1 << endl;
qsort(SArray, 4, sizeof(MyString), CompareString);
for (int i = 0; i < 4; i++)
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0, 4) << endl;//operator()
//s1的从下标5开始长度为10的子串
cout << s1(5, 10) << endl;
return 0;
}