Hello, I have a problem, I cant change my arrays dynamically.
I'm using Codeblocks for IDE and mingw for the compiler.
this is how my header looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <array>
#include <iostream>
#include "vector.h"
usingnamespace std;
class Curve
{
public:
void LoadCurve(array<Vector2,1> curve);
float evaluate(float position);
private:
int p;
int valuesLen;
array<float, 2> values;
int FindPoint(float position);
};
and my .cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Curve.h"
void Curve::LoadCurve(array<Vector2,1> curve)
{
int len = curve.size();
values = new array<float, 2>(len,2);
for (int i = 0; i < len; i++)
{
values[i,0] = curve.x[i];
values[i,1] = curve.y[i];
}
valuesLen = len;
}
and my errors look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
C:\MoDyEnDLL\Curve.cpp||In member function 'int Curve::FindPoint(float)':|
C:\MoDyEnDLL\Curve.cpp|9|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\MoDyEnDLL\Curve.cpp||In member function 'void Curve::LoadCurve(std::array<Vector2, 1u>)':|
C:\MoDyEnDLL\Curve.cpp|20|error: new initializer expression list treated as compound expression [-fpermissive]|
C:\MoDyEnDLL\Curve.cpp|20|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\MoDyEnDLL\Curve.cpp|20|error: no matching function for call to 'std::array<float, 2u>::array(int)'|
C:\MoDyEnDLL\Curve.cpp|20|note: candidates are:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\array|62|note: std::array<float, 2u>::array()|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\array|62|note: candidate expects 0 arguments, 1 provided|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\array|62|note: constexpr std::array<float, 2u>::array(const std::array<float, 2u>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\array|62|note: no known conversion for argument 1 from 'int' to 'const std::array<float, 2u>&'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\array|62|note: constexpr std::array<float, 2u>::array(std::array<float, 2u>&&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\array|62|note: no known conversion for argument 1 from 'int' to 'std::array<float, 2u>&&'|
C:\MoDyEnDLL\Curve.cpp|24|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\MoDyEnDLL\Curve.cpp|24|error: 'struct std::array<Vector2, 1u>' has no member named 'x'|
C:\MoDyEnDLL\Curve.cpp|25|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\MoDyEnDLL\Curve.cpp|25|error: 'struct std::array<Vector2, 1u>' has no member named 'y'|
||=== Build finished: 4 errors, 4 warnings (0 minutes, 0 seconds) ===|
What should I do to resolve this issue?
Basically what I want to achive is quite simple. I wrote this code in C# and i'm trying to recrete it in c++. I tryed to use a c# to c++ converter but the code was not working.
You cannot supply two indices to a list like that:
values[i,0] = ...
Your method is also not being careful to properly delete[] any previous value of values, if any.
I am also unsure why you are using std::array here. (IMHO, it is a fairly useless class...) In any case, it is for fixed size data, not dynamic. If you want a dynamic array, you should be using a std::vector. To resize the data, all you need do is call values.resize( len );. No pointers involved.
Thanks for the reply, well my problem is that I wanted to create a multidimensional array. In my particular case(Car engine data), Rpm in the first array and Engine torque in the second. (Actually I want more engine data in the array, but as for now I have 2 "row's")
Anyway as I searched google for it, it seemed like Arrays are simple to use). Or could you sugest a page with a great example on how to create multidimensional arrays and modify the sizes & data dynamically. Tryed to use the native way but that also didnt seem to cut the deal.