I am stumped. Please help.
What is invalid about this code?
class Row
{
private:
const int numSamples;
int samples[numSamples];
//int samples[2]; //this line compiles fine
public:
Row(const int ns): numSamples(ns) { }
};
Row row(2);
void setup()
{
}
void loop()
{
}
output:
Row:4: error: invalid use of non-static data member 'Row::numSamples'
Row:5: error: from this location
Row:5: error: array bound is not an integer constant
Got this error on Arduino 1.0.5-r2 and Arduino 1.0.6
I don't think C++11 is going to help. I compiled the following with MinGW.
/* this fails with:
g++ Row.cpp
g++ -Wall -std=c++11 Row.cpp
*/
class Row
{
private:
const int numSamples; //error: invalid use of non-static data member 'Row::numSamples'
int samples[numSamples]; //error: array bound is not an integer constant
public:
Row(const int ns): numSamples(ns) { }
};
Row row(2);
int main()
{
}
/* this compiles */
template< unsigned N >
class Row{
private:
int samples[ N ];
};
Row< 2 > row;
int main()
{
}
Yeah, not like you have it, your code will have to change. Constexpr will allow you to use return values / parameters as compile time constants. However your constructor has nothing to do with the line of code that sizes the array.
As the input is constant, there may be no need for a class as you can do quite a bit using constexpr functions. However if the storage is required ( can't act on data as it happens ) then there is not much you can do unless you want to start using dynamic memory.
pYro_65,
constexpr looks interesting.
How to make constexpr compile in this example?:
class Row
{
private:
constexpr int numSamples;//error: non-static data member 'numSamples' declared 'constexpr'
int samples[numSamples]; //error: invalid use of non-static data member 'Row::numSamples'
public:
Row(const int ns): numSamples(ns) { }
};
Row row(2);
int main()
{
}
Output:
C:\demo_MinGW>g++ -Wall -std=c++11 Row.cpp
Row.cpp:23:16: error: non-static data member 'numSamples' declared 'constexpr'
constexpr int numSamples;
^
Row.cpp:23:16: error: invalid use of non-static data member 'Row::numSamples'
Row.cpp:24:14: error: from this location
int samples[numSamples];
^
Hi PaulS.
The Row class will be in a library. Row will be used by various .ino programs.
The size of the sample array is a constant value determined by the .ino program.
This example shows Row with two elements in the samples array:
#include <iostream>
template< unsigned N > //number of samples
class Row{
private:
int samples[ N ]; //array of samples
public:
void scan()
{
for (int i=0; i<N; i++)
{
samples[i] = i;
std::cout << samples[i];
}
}
};
const int N=2;
Row< N > row;
int main()
{
row.scan();
}
The number of samples is a constant known at compile time.
So using template to set array size avoids the overhead of dynamically allocate the array.
Sorry I did not give good answer to your first question before.
The life of Row objects is until end of program, so ~Row destructor is not needed.