返回原始子字符串的子字符串方法 [英] Substring method that return substring of the original
问题描述
我要求创建一个 Substring()
方法,返回从开始的原始字符串的子字符串
这是我如何尝试实现我的.cpp文件中的函数:
MyString sub;
sub = new char [length];
for(int i = start; i {
sub [i] = this [i]
}
return sub;
我收到此错误:
错误:
之前的期望未限定标识符 c <$>
MyString.cpp:206:错误:在
sub [i]
注意:我不应该重载[]。
MyString是定义的类。
我做错了什么?
这只是意味着this.substr()不是有效的C ++。
这是一个指向当前对象的指针。因此,除非方法MyString :: substr()存在,否则不能这样做。
从那开始,我不知道你的MyString中有哪些成员类。如果有一个std :: string,可以使用substr方法。 f底层成员只是一个char *,你将不得不使用简单的数组和c字符串操作来从中提取一个subtring。
有一个例子。如果你的类是这样
class MyString
{
private:
std :: string _str;
public:
//构造函数
MyString(std :: string str);
//你的方法
MyString Substring(int start,int length)const;
};
然后你的Substring方法将是这样:
MyString MyString :: Substring(int start,int length)const
{
return MyString(_str.substr(start,length)另一方面,如果你的类MyString是这样的:)
$ b >
class MyString
{
private:
char * _str;
public:
//构造函数
MyString(char * str);
//你的方法
MyString Substring(int start,int length)const;
};然后你的方法将是这样:
MyString MyString :: Substring(int start,int length)const
{
char * res_str = new char [length + 1];
memcpy(res_str,(char *)_str + start,length);
res_str [length] ='\0';
return MyString(res_str);
}
编辑:如果我们看看你提供的代码看来你实际上是使用底层的char *。让我们来看看你写的是什么^^
MyString sub;
sub = new char [length];
你想做的就是修改底层char *的字符。所以你应该做的是:
char * sub;
sub = new char [length];
因此,不是创建新的MyString,而是创建一个新的char *。你不能直接分配一个char *到一个MyString(或至少,这是我想)。现在,让我们来看看代码的另一部分:
for(int i = start; i< length; i ++)
{
sub [i] = this [i];
}
这是一个指向MyString的指针。所以这个[i]相当于this.operator是incalid,因为这是一个指针。你不能有一个后面跟一个点。然而,如果你写了this->运算符,它会搜索一个函数,如char& MyString :: operator [](int i)。因为你没有定义这个函数,你仍然会有一个编译器错误(它也是一个你现在有sub [i],因为你定义sub作为一个MyString。你应该写:
for(int i = start; i {
sub [i] = _str [i];
}
但它仍然提供_str是一个char *在你的类,然后你将能够完成您的功能:
return MyString(sub);
但是,假设你的MyString类有一个构造函数,它使用一个char *作为参数:)
I am asked to create a Substring()
method that returns a substring of the original string which begins at location start
and is as long as length
Here is how I attempted to implement the function in my .cpp file:
MyString sub;
sub = new char[length];
for(int i = start; i <length; i++)
{
sub[i] = this[i];
}
return sub;
and I got this error:
error: expected unqualified-id before [
token
MyString.cpp:206: error: no match for operator[]
in sub[i]
Note: I am not supposed to overload [].
MyString is the defined class.
What exactly am I doing wrong?
解决方案 It simply means that this.substr() is not valid C++.
this is a pointer to the current object. So unless the method MyString::substr() exists, you can't do that.
Starting from that, I don't know which members there are in your MyString class. If there is a std::string, you can use the substr method on it. f the underlying member is just a char*, you will have to use simple array and c-string operations on it to extract a subtring from it.
With an exemple. If your class is like this
class MyString
{
private:
std::string _str;
public:
// Constructor
MyString(std::string str);
// Your method
MyString Substring(int start, int length) const;
};
Then your Substring method will be like this:
MyString MyString::Substring(int start, int length) const
{
return MyString(_str.substr(start, length));
}
On the other hand, if your class MyString is like that:
class MyString
{
private:
char* _str;
public:
// Constructor
MyString(char* str);
// Your method
MyString Substring(int start, int length) const;
};
Then your method will be like this:
MyString MyString::Substring(int start, int length) const
{
char* res_str = new char[length+1];
memcpy(res_str, (char*) _str + start, length);
res_str[length] = '\0';
return MyString(res_str);
}
EDIT : If we look at the code you provided (after last edit), it seems that you are actually using an underlying char*. So let's have a look at what you wrote^^
MyString sub;
sub = new char[length];
What you want to do is actually modify the characters of the underlying char*. So what you should have done is:
char* sub;
sub = new char[length];
So instead of creating a new MyString, you will create a new char*. You can't directly assign a char* to a MyString (or at least, it's what I think). Now, let's look a the other part of your code:
for(int i = start; i <length; i++)
{
sub[i] = this[i];
}
this is a pointer to MyString. So this[i] is equivalent to this.operator which is incalid since this is a pointer. You can't have this followed by a dot. However, if you had written this->operator, it would have searched for a function like char& MyString::operator[](int i). Since you did not defined this function, you would still have a compiler error (it's also the one you currently have for sub[i] since you defined sub as a MyString. You should write:
for (int i = start; i < length; i++)
{
sub[i] = _str[i];
}
But it's still provided _str is a char* in your class. Then you will be able to finish your function by:
return MyString(sub);
But there, it's provided that your MyString class has a constructor that takes a char* as a parameter :)
这篇关于返回原始子字符串的子字符串方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!