gpt4 book ai didi

c++ - 运算符重载、奇段错误、类、C++

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:51 27 4
gpt4 key购买 nike

我的导师给了我一个关于重载运算符的练习。他给了我不能更改的 int main() 函数。我应该编写函数等,以便代码可以工作。不幸的是我有段错误。我注意到如果

 TSeries series4=series1(2,4);
cout << "Series4: " << series4 << endl;

行被评论它或多或少工作。我将非常感谢你的帮助。

#include <iostream>
class TSeries {
public:
TSeries()
{
_size = 0;
_capacity = 0;
_tab = NULL;
}


TSeries(float *tab, const int size)
{
_tab = new float[size];
for (int i = 0; i < size; i++) _tab[i] = tab[i];
_size = size;
_capacity = 0;
}

~TSeries() { delete[] _tab; }

TSeries & operator+=(float value) { return insert(value); }
TSeries & operator,(float value) { return insert(value); }
TSeries & operator+(const TSeries & s)
{
// if(this->_size != s._size) std::cout<<"Size doesn't match!"<<std::endl;
/*else
{
std::cout<<"whee";
for(int i; i<this->_size;i++)
{
//this->_tab[i] += s._tab[i];
std::cout<<"nothing";
}
return *this;
}*/
//std::cout<<"sth";
}

TSeries & operator()(int position1, int position2)
{
// return *this;
}

TSeries & insert(float k)
{
if (_size >= _capacity) Enlarge();
_tab[_size++] = k;
return *this;
}
friend std::ostream & operator<<(std::ostream & out, const TSeries & s);


private:
int _size, _capacity;
float *_tab, *_itr;
static int _nr;

void Enlarge()
{
_capacity = 2 * _capacity + 1;
float *tmp = new float[_capacity];

for (int i = 0; i < _size; ++i)
{
tmp[i] = _tab[i];
}
delete[] _tab;
_tab = tmp;
}
};

std::ostream & operator<<(std::ostream & out, const TSeries & s)
{
int przedostatni = s._size - 1;
out << "(";
for (int i = 0; i < s._size; i++)
{
out << (int)s._tab[i];
if (i != przedostatni)
out << ",";
}
out << ")" << std::endl;
}

using namespace std;
int main(int argc, char **argv) {
TSeries series1;
series1 += 1., 2., 4., 2.;
cout << "Series1: " << series1 << endl;

const int size = 7;
float tab[size] = { 3.,3.,3.,4.,5.,1.,0. };
const TSeries series2(tab, size);
cout << "Series2: " << series2 << endl << endl;


TSeries series3 = series1 + series2;
cout << "Series3: " << series3 << endl << endl;

series1 += 1., 0., 3.;
series3 = series1 + series2;
cout << " " << series1 << endl;
cout << " +" << series2 << endl;
cout << " ---------------------" << endl;
cout << "Series3: " << series3 << endl << endl;

TSeries series4 = series1(2, 4);
cout << "Series4: " << series4 << endl;

return 0;
}

/* output required:
Series1: (1,2,4,2)
Series2: (3,3,3,4,5,1,0)

Size doesn't match!
Series3: ()

(1,2,4,2,1,0,3)
+(3,3,3,4,5,1,0)
---------------------
Series3: (4,5,7,6,6,1,3)

Series4: (4,2)
*/

最佳答案

  1. std::ostream & operator<<(std::ostream & out, const TSeries & s)不返回任何东西。请加一个return out在函数结束时
  2. operator()operator+都应该以 return *this 结尾
  3. 正如 Devolus 已经指出的:您没有复制构造函数,也没有 operator=(const TSeries&)定义
  4. 您可以使用 memcpy更快地复制数组。#
  5. ioperator+(const TSeries&) 的 for 循环中未初始化。
  6. 你的 operator(int, int)当前确实改变了原始对象。这似乎不对。

代码:

#include <iostream>
class TSeries {
public:
TSeries()
{
_size = 0;
_capacity = 0;

_tab = NULL;
}


TSeries(float *tab, const int size)
{
_size = size;
_capacity = 0;

_tab = new float[size];
memcpy(_tab, tab, _size*sizeof(float));
}

TSeries(const TSeries& other)
{
_size = other._size;
_capacity = other._capacity;

_tab = new float[_size];
memcpy(_tab, other._tab, _size*sizeof(float));
}

~TSeries()
{
delete[] _tab;
}

TSeries & operator+=(float value) { return insert(value); }
TSeries & operator,(float value) { return insert(value); }
TSeries & operator+(const TSeries & other)
{
if (this->_size != other._size)
{
std::cout << "Size doesn't match!" << std::endl;
}
else
{
//std::cout << "whee";
for (int i = 0; i < this->_size; i++)
{
_tab[i] += other._tab[i];
//std::cout << "nothing";
}
}
//std::cout<<"sth";
return *this;
}

TSeries& operator=(const TSeries& other)
{
_size = other._size;
_capacity = other._capacity;
//Create tmp in case of self-assignment
float *tmp = new float[_capacity];
memcpy(tmp, other._tab, _size*sizeof(float));
delete[] _tab;
_tab = tmp;
return *this;
}

TSeries operator()(int position1, int position2)
{
//TODO: Range-Check
return TSeries(_tab + position1, position2 - position1);
}

TSeries & insert(float k)
{
if (_size >= _capacity) Enlarge();
_tab[_size++] = k;
return *this;
}
friend std::ostream & operator<<(std::ostream & out, const TSeries & s);


private:
int _size, _capacity;
float *_tab, *_itr;
static int _nr;

void Enlarge()
{
_capacity = 2 * _capacity + 1;
float *tmp = new float[_capacity];
memcpy(tmp, _tab, _size*sizeof(float));
delete[] _tab;
_tab = tmp;
}
};

std::ostream & operator<<(std::ostream & out, const TSeries & s)
{
int przedostatni = s._size - 1;
out << "(";
for (int i = 0; i < s._size; i++)
{
out << (int)s._tab[i];
if (i != przedostatni)
out << ",";
}
out << ")" << std::endl;
return out;
}

using namespace std;
int main(int argc, char **argv) {
TSeries series1;
series1 += 1., 2., 4., 2.;
cout << "Series1: " << series1 << endl;

const int size = 7;
float tab[size] = { 3.,3.,3.,4.,5.,1.,0. };
const TSeries series2(tab, size);
cout << "Series2: " << series2 << endl << endl;


TSeries series3 = series1 + series2;
cout << "Series3: " << series3 << endl << endl;

series1 += 1., 0., 3.;
series3 = series1 + series2;
cout << " " << series1 << endl;
cout << " +" << series2 << endl;
cout << " ---------------------" << endl;
cout << "Series3: " << series3 << endl << endl;

TSeries series4 = series1(2, 4);
cout << "Series4: " << series4 << endl;

return 0;
}

/* output required:
Series1: (1,2,4,2)
Series2: (3,3,3,4,5,1,0)

Size doesn't match!
Series3: ()

(1,2,4,2,1,0,3)
+(3,3,3,4,5,1,0)
---------------------
Series3: (4,5,7,6,6,1,3)

Series4: (4,2)
*/

输出

Series1: (1,2,4,2)

Series2: (3,3,3,4,5,1,0)


Size doesn't match!
Series3: (1,2,4,2)


(4,5,7,6,6,1,3)

+(3,3,3,4,5,1,0)

---------------------
Series3: (4,5,7,6,6,1,3)


Series4: (7,6)

更新:

你的 operator+(const TSeries &)应该看起来像这样:

TSeries operator+(const TSeries & other)
{
if (this->_size != other._size)
{
std::cout << "Size doesn't match!" << std::endl;
return TSeries(); //Return empty object
}
TSeries tmp(*this); //Create copy
for (int i = 0; i < tmp._size; i++)
{
tmp._tab[i] += other._tab[i];
}
return tmp;
}

还有你的operator()(int, int)像这样:

TSeries operator()(int position1, int position2)
{
if (position1 < 0) position1 = 0;
else if (position1 >= _size) position1 = _size - 1;

if (position2 < position1) position2 = position1;
else if (position2 >= _size) position2 = _size - 1;

return TSeries(_tab + position1, position2 - position1);
}

也许您想在错误情况下抛出异常?

关于c++ - 运算符重载、奇段错误、类、C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33626540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com