Wal*_*ter 1 c++ operator-overloading
我目前正处于拼贴二级编程课程......我们正在研究运算符重载...要做到这一点,我们要重建矢量类...我正在构建类,发现它的大部分是基于[] operator.当我试图实现时,+ operator我遇到了一个奇怪的错误,我的教授以前没见过(显然是因为班级将IDE从MinGW切换到VS表达...)(我正在使用Visual Studio Express 2008 C++版......)
#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
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
out << n[i] << " "; error C2678:
Run Code Online (Sandbox Code Playgroud)
二进制'[':没有找到哪个运算符采用'const Vector'类型的左手操作数(或者没有可接受的转换)
return newone;
Run Code Online (Sandbox Code Playgroud)
错误C2106:'=':左操作数必须是l值
如上所述,我是一名学生进入计算机科学,因为我选择的专业我会很感激提示,指针和更好的方法来做事:D
Jam*_*lis 10
这个:
int operator[ ](int);
Run Code Online (Sandbox Code Playgroud)
是一个非const成员函数.这意味着它不能被调用const Vector.
通常,下标运算符被实现为返回一个引用(如果你返回一个值,就像你正在做的那样,你不能将它用作左值,例如你不能newone[j] = temp_2_int;像你的代码那样):
int& operator[](int);
Run Code Online (Sandbox Code Playgroud)
为了能够在const对象上调用它,您还应该提供成员函数的const版本:
const int& operator[](int) const;
Run Code Online (Sandbox Code Playgroud)
既然你要求"提示,指针和更好的方法来做事:"
_VECTOR_H.以下划线后跟大写字母开头的名称将保留用于实现.有很多关于下划线规则.using namespace std在标题中使用.operator+应该采取一个,const Vector&因为它不会修改它的论点.at应该返回一个int并且应该匹配C++标准库容器的语义(即,如果i超出界限它应该抛出异常.你需要(*this)[i]用来调用你的重载operator[].*操作员的操作.在一些地方,你已经混淆了指针和它们所指向的对象.=与==(例如if (comp = 0))混淆.编译器会警告你这件事.不要忽视警告.data永远不会为NULL ,那么您的逻辑将更加简单.