[英]I can't assign a value to a 2d array
So this is my first attempt to write a program using classes in c++.所以这是我第一次尝试使用 C++ 中的类编写程序。 As an exercise, I'm trying to create a 2d-array class that can do certain things.
作为练习,我正在尝试创建一个可以执行某些操作的二维数组类。 This is a part of the code in the main.cpp that my class has to be able to run.
这是 main.cpp 中我的类必须能够运行的代码的一部分。
int main()
{
array2d<double> ar(2,2);
ar(1,1) = 1;
ar(0,0) = 2;
ar(0,1) = 3;
ar(1,0) = 4;
auto X{ar};
array2d<double> c(2,2);
c = ar;
c.diagonal(1) = 5; //c(1,1) = 5
}
And this is my code.这是我的代码。 I'm getting an error in the diagonal function "error: lvalue required as left operand of assignment".
我在对角函数中遇到错误“错误:左值需要作为赋值的左操作数”。 I can't figure out what I should do.
我不知道我应该做什么。 The purpose of this function is to assign values in the diagonal elements of the array.
此函数的目的是在数组的对角线元素中分配值。
template<typename T>
class array2d
{
private:
int n,m;
vector<T> vec;
public:
array2d(int M, int N)
: n{N}, m{M}
{
vector<T> vec1(n*m);
vec = vec1;
}
array2d(array2d<T> &arr)
: n{arr.N()} , m{arr.M()}
{
vec = arr.V();
}
array2d &operator=(array2d<T> & arr){
vec = arr.V();
return *this;
}
T &operator()(int i,int j)
{
auto it = vec.begin();
it += i*n+j;
return *it;
}
T diagonal(int a)
{
auto x= *this;
return x(a,a);
}
int M (){return m;}
int N (){return n;}
vector<T> V(){return vec;}
};
Edit: I have another problem.编辑:我还有一个问题。 Next thing my class should be able to run is this
ar = X.transpose();
我的班级应该能够运行的下一件事是这个
ar = X.transpose();
But I'm getting 2 errors:但我收到 2 个错误:
error: invalid initialization of non-const reference of type 'array2d&' from an rvalue of type 'array2d' AND错误:从类型的右值类型的“array2d&”非const引用无效初始化“array2d” AND
note: initializing argument 1 of 'array2d array2d::operator=(array2d&) [with T = double]'|注意:初始化 'array2d array2d::operator=(array2d&) [with T = double]'| 的参数 1
My function is this.我的功能是这个。
array2d<T> transpose ()
{
auto &y = *this ;
array2d<T> x(y.N(),y.M()) ;
for (int i{0};i<x.d1();i++){
for (int j{0}; j<x.d2();j++){
x(i,j) = y(j,i);
}
};
return x;
}
I made miscellaneous changes and have commented in the code:我进行了各种更改并在代码中进行了注释:
// bad practice:
// using namespace std;
template<typename T>
class array2d {
private:
// use an unsigned int for dimensions
unsigned m, n;
std::vector<T> vec;
public:
// initialize the vector in the member initializer list too:
array2d(unsigned M, unsigned N) : m{M}, n{N}, vec(m * n)
{}
// arr should be a const&:
array2d(const array2d<T>& arr) : m{arr.m}, n{arr.n}, vec(arr.vec)
{}
// arr should be a const&:
array2d& operator=(const array2d<T>& arr) {
vec = arr.vec;
// n and m should also be copied:
m = arr.m;
n = arr.n;
return *this;
}
T& operator()(unsigned i, unsigned j) {
// unnecessary iterator arithmetic replaced
return vec[i * n + j];
}
// a const version to be used in const contexts:
const T& operator()(unsigned i, unsigned j) const {
return vec[i * n + j];
}
// You should return a T& and it should be a reference to
// a T inside vec, not to a copy that will become invalid as soon as the function
// returns.
T& diagonal(unsigned a) {
return (*this)(a, a);
}
// this function should be const since you are not supposed to change "this"
array2d<T> transpose() const {
// y is just confusing
// auto& y = *this;
// Use your member variables (or call the functions M() and N()) directly.
array2d<T> x(n, m);
// d1() and d2() doesn't exist in the code you've shown, but they are not
// needed:
for(unsigned i = 0; i < x.m; i++) {
for(unsigned j = 0; j < x.n; j++) {
x(i, j) = (*this)(j, i);
}
}
return x;
}
unsigned M() { return m; }
unsigned N() { return n; }
std::vector<T> V() { return vec; }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.