Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
此外,代码重载无效 我现在正在读一本C++的老书,在其中开发了一个“Rational数”类来介绍操作符重载的概念等。这里有一些书中的例子代码:_C++_String_Operator Overloading - Fatal编程技术网

此外,代码重载无效 我现在正在读一本C++的老书,在其中开发了一个“Rational数”类来介绍操作符重载的概念等。这里有一些书中的例子代码:

此外,代码重载无效 我现在正在读一本C++的老书,在其中开发了一个“Rational数”类来介绍操作符重载的概念等。这里有一些书中的例子代码:,c++,string,operator-overloading,C++,String,Operator Overloading,接口: const Rational operator+(const Rational& Rhs) const; 实施: const Rational Rational::operator+(const Rational& Rhs) const { Rational Answer(*this); Answer += Rhs; return Answer; } const String operator+(const String&) const

接口

const Rational operator+(const Rational& Rhs) const;
实施:

const Rational Rational::operator+(const Rational& Rhs) const
{
    Rational Answer(*this);
    Answer += Rhs;
    return Answer;
}
const String operator+(const String&) const;
const String String::operator+(const String& Rhs) const
{
    String Answer (*this);
    Answer += Rhs;
    return Answer;
}
复制构造函数执行您认为它会执行的操作,并且+=运算符正确重载

我决定通过实现一个string类来进行一些练习,因此我采用了类似的方法。我的
+=
重载工作正常,但
+
似乎最终没有效果

界面:

const Rational Rational::operator+(const Rational& Rhs) const
{
    Rational Answer(*this);
    Answer += Rhs;
    return Answer;
}
const String operator+(const String&) const;
const String String::operator+(const String& Rhs) const
{
    String Answer (*this);
    Answer += Rhs;
    return Answer;
}
实施:

const Rational Rational::operator+(const Rational& Rhs) const
{
    Rational Answer(*this);
    Answer += Rhs;
    return Answer;
}
const String operator+(const String&) const;
const String String::operator+(const String& Rhs) const
{
    String Answer (*this);
    Answer += Rhs;
    return Answer;
}
如果复制构造函数(工作)被定义为:

String::String(const String& str)
{
    unsigned _strlen = str.len() + 1;
    content = new char[_strlen];
    std::memcpy(content, str.content, _strlen);
    length = _strlen - 1;
    content[length] = '\0';
}
+=
被以下内容重载:

const String& String::operator+=(const String& Rhs)
{
    unsigned _Addl = Rhs.len();
    unsigned newLen = _Addl + length;  //length is member variable -- current length of content

    content = (char*) realloc( content, newLen+1 );

    std::memcpy(content+length, Rhs.content, _Addl);
    content[newLen] = '\0';

    return *this;
}
但是--虽然我可以为
+=
获得正确的输出,
+
运算符实际上无法返回连接的字符串。在函数内部调试输出时,
Answer
保存正确的内容,但它返回原始字符串,而不是连接的字符串。我有一种感觉,这与
const
无处不在有关,但我也尝试过没有它也没有好运

测试代码:

(主要内容):


您的
String::operator+=()
,尽管您声称它已正确实现,但未正确实现

首先,
realloc()

第二,也是更关键的一点是,
length
成员没有被更新。由于您的代码调用
len()
成员函数来获取一个字符串的长度,并使用
length
成员来获取另一个字符串的长度,因此所有函数都需要确保这两个方法同步(即,它们为
string
的给定实例提供一致的结果)。由于未更新
length
,因此您的代码无法确保

可能还有比使用C型内存分配更好的方法,但是(假设这是一个学习练习),我不谈这个


您没有为您的
Rational
类提供任何相关的代码,但是,如果它不工作,您的代码可能会在各种构造函数和成员函数之间表现出类似的不一致性。

您的
字符串::运算符+=()
,尽管您声称它已正确实现,但未正确实现

首先,
realloc()

第二,也是更关键的一点是,
length
成员没有被更新。由于您的代码调用
len()
成员函数来获取一个字符串的长度,并使用
length
成员来获取另一个字符串的长度,因此所有函数都需要确保这两个方法同步(即,它们为
string
的给定实例提供一致的结果)。由于未更新
length
,因此您的代码无法确保

可能还有比使用C型内存分配更好的方法,但是(假设这是一个学习练习),我不谈这个


你没有给你的代码> Realth类提供相关代码,但是如果它不工作,你的代码可能在各种构造函数和成员函数之间表现出类似的不一致性。

显示你的测试代码。顺便说一下,C++上的旧书可能不是你的时间的好投资。他们会教你过时的技术在C++程序中使用<代码> MALLC/<代码>?为什么?@PaulMcKenzie不好的风格,因为我来自C。真丢脸,我guess@jaska您未能更新
长度
成员变量。如果
realloc
返回NULL,您将怎么做?您的代码将显示未定义的行为,因为它将继续前进并尝试执行函数的其余部分。另一方面,如果使用<代码> NeX[]/C++ >,将在错误上抛出异常,并且不会执行后续代码(因此,您的对象不会被破坏)。显示您的测试代码。他们会教你过时的技术在C++程序中使用<代码> MALLC/<代码>?为什么?@PaulMcKenzie不好的风格,因为我来自C。真丢脸,我guess@jaska您未能更新
长度
成员变量。如果
realloc
返回NULL,您将怎么做?您的代码将显示未定义的行为,因为它将继续前进并尝试执行函数的其余部分。另一方面,如果您使用了
new[]
,则会在出错时引发异常,并且不会执行后续代码(因此您的对象不会损坏)。实际上,这似乎已经修复了它。我还更新了使用
new
而不是
malloc
,并且我正在使用一个临时的
char*
来摆脱
realloc
用法。实际上,这似乎已经解决了它。我还更新了使用
new
而不是
malloc
,并且我正在使用一个临时的
char*
来摆脱
realloc
用法。