gpt4 book ai didi

C++ 运算符重载 - 'recreating the Vector'

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:08:59 28 4
gpt4 key购买 nike

我目前正在上大学二级编程类(class)...我们正在研究运算符重载...为此我们将重建 vector 类...我正在构建类,发现其中大部分是基于 [] 运算符。当我尝试实现 + operator 时,我遇到了一个我的教授以前从未见过的奇怪错误(显然是因为该类(class)将 IDE 从 MinGW 切换到 VS express...)(我正在使用 Visual Studio Express 2008 C++ 版...)

vector .h

#include <string>
#include <iostream>
using namespace std;

#ifndef _VECTOR_H
#define _VECTOR_H

const int DEFAULT_VECTOR_SIZE = 5;

class Vector
{
private:
int * data;
int size;
int comp;
public:
inline Vector (int Comp = 5,int Size = 0)
: comp(Comp), size(Size) { if (comp > 0) { data = new int [comp]; }
else { data = new int [DEFAULT_VECTOR_SIZE];
comp = DEFAULT_VECTOR_SIZE; }
}
int size_ () const { return size; }
int comp_ () const { return comp; }
bool push_back (int);
bool push_front (int);
void expand ();
void expand (int);
void clear ();
const string at (int);
int& operator[ ](int);
int& operator[ ](int) const;
Vector& operator+ (Vector&);
Vector& operator- (const Vector&);
bool operator== (const Vector&);
bool operator!= (const Vector&);

~Vector() { delete [] data; }
};

ostream& operator<< (ostream&, const Vector&);

#endif

vector .cpp

#include <iostream>
#include <string>
#include "Vector.h"
using namespace std;

const string Vector::at(int i) {
this[i];
}

void Vector::expand() {
expand(size);
}

void Vector::expand(int n ) {
int * newdata = new int [comp * 2];
if (*data != NULL) {
for (int i = 0; i <= (comp); i++) {
newdata[i] = data[i];
}
newdata -= comp;
comp += n;
data = newdata;
delete newdata;
}
else if ( *data == NULL || comp == 0) {
data = new int [DEFAULT_VECTOR_SIZE];
comp = DEFAULT_VECTOR_SIZE;
size = 0;
}
}

bool Vector::push_back(int n) {
if (comp = 0) { expand(); }
for (int k = 0; k != 2; k++) {
if ( size != comp ){
data[size] = n;
size++;
return true;
}
else {
expand();
}
}
return false;
}

void Vector::clear() {
delete [] data;
comp = 0;
size = 0;
}
int& Vector::operator[] (int place) { return (data[place]); }
int& Vector::operator[] (int place) const { return (data[place]); }

Vector& Vector::operator+ (Vector& n) {
int temp_int = 0;

if (size > n.size_() || size == n.size_()) { temp_int = size; }
else if (size < n.size_()) { temp_int = n.size_(); }

Vector newone(temp_int);
int temp_2_int = 0;

for ( int j = 0; j <= temp_int &&
j <= n.size_() &&
j <= size;
j++) {
temp_2_int = n[j] + data[j];
newone[j] = temp_2_int;
}
////////////////////////////////////////////////////////////
return newone;
////////////////////////////////////////////////////////////
}

ostream& operator<< (ostream& out, const Vector& n) {
for (int i = 0; i <= n.size_(); i++) {
////////////////////////////////////////////////////////////
out << n[i] << " ";
////////////////////////////////////////////////////////////
}
return out;
}

错误:

out << n[i] << " "; error C2678:

二进制“[”:未找到运算符采用类型的左侧操作数'const Vector'(或者没有可接受的转换)

return newone;

错误 C2106:“=”:左操作数必须是左值


如上所述,我是一名进入计算机科学专业的学生,​​我希望得到提示、指示和更好的做事方法 :D

最佳答案

这个:

int operator[ ](int);

是非常量成员函数。这意味着它不能在 const Vector 上调用。

通常,下标运算符被实现为返回一个引用(如果你返回一个值,就像你正在做的那样,你不能将它用作左值,例如你不能做 newone[j ] = temp_2_int; 就像你在你的代码中一样):

int& operator[](int);

为了能够在 const 对象上调用它,您还应该提供成员函数的 const 版本:

const int& operator[](int) const;

由于您要求“提示、指导和更好的做事方式:”

  • 你不能命名你的 include guard _VECTOR_H。以下划线开头后跟大写字母的名称保留用于实现。有a lot of rules about underscores .
  • 永远不要在 header 中使用using namespace std
  • 您的 operator+ 应该采用 const Vector&,因为它不会修改其参数。
  • 你的 at 应该返回一个 int 并且应该匹配 C++ 标准库容器的语义(即,如果 i 超出范围。您需要使用 (*this)[i] 来调用重载的 operator[]
  • 您需要了解* 运算符的作用。在几个地方,您混淆了指针和它们指向的对象。
  • 注意将 === 混淆(例如,在 if (comp = 0) 中)。编译器就此警告您。不要忽视警告。
  • 如果您保证 data 永远不会为 NULL,您的逻辑将会简单得多。

关于C++ 运算符重载 - 'recreating the Vector',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2567784/

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