我必须实现一个Vector类,它设置一个多维向量的坐标,并在使用这个特定的代码调用时工作(我不能改变这部分):
const int NumOfDimensions = 5;
Vector x (NumOfDimensions);
x.Set(0, 1.1).Set(1, 1.2).Set(2, 1.3).Set(3, 1.4).Set(4, 1.5);
x.print();
Run Code Online (Sandbox Code Playgroud)
输出必须是这样的:
(1.1,1.2,1.3,1.4,1.5)
这是我尝试但无法让它工作:
class Vector {
float *coordinates;
int dimensions;
public:
Vector(int k)
{
coordinates = new float[k];
dimensions = k;
}
void Set(int k, float wsp)
{
//Vector x(k+1);
coordinates[k] = wsp;
//return x;
}
void print()
{
int i;
cout<<"(";
for(i=0; i<dimensions; i++)
cout<<coordinates[i]<<", ";
cout<<")"<<endl;
}
};
Run Code Online (Sandbox Code Playgroud)
所以我知道函数Set需要改变并且可能返回一个对象,但我尝试了很多不同的方法而且它不起作用.我应该如何修改它?