array [variable] problem

I'm stumped.

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
//...
//...
struct function {
	int degree;
	float coeff [degree];
	float constant;
} ax, bx, cx, dx, ex, fx, gx, hx, ix, jx;

Does not compile. Error:
1
2
3
4
1>  graphcalc.cpp
1>c:\users\jill\documents\visual studio 2010\projects\graphic calculator\graphcalc.cpp(22): error C2327: 'function::degree' : is not a type name, static, or enumerator
1>c:\users\jill\documents\visual studio 2010\projects\graphic calculator\graphcalc.cpp(22): error C2065: 'degree' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Fair enough, changed int degree; to int degree = 1;

It doesn't compile either.
1
2
3
4
5
1>  graphcalc.cpp
1>c:\users\jill\documents\visual studio 2010\projects\graphic calculator\graphcalc.cpp(21): error C2864: 'function::degree' : only static const integral data members can be initialized within a class
1>c:\users\jill\documents\visual studio 2010\projects\graphic calculator\graphcalc.cpp(22): error C2327: 'function::degree' : is not a type name, static, or enumerator
1>c:\users\jill\documents\visual studio 2010\projects\graphic calculator\graphcalc.cpp(22): error C2065: 'degree' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What's the problem?

Thanks!
closed account (zb0S216C)
The length of a C-Style array must be a compile time constant so that the array can be allocated at compile time. function::degree needs to be constant in order for it to be used as function::coeff's array length.

Wazzak
Last edited on
Just in case you didn't get it, here is an example of what Framework just said:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
//...
//...
    const int degree=1;
struct function {
	float coeff [degree];
	float constant;
} ax, bx, cx, dx, ex, fx, gx, hx, ix, jx;
Just to make sure I get it;

Array lengths must be determined when you compile the code. Arrays can not be created while the program is running. Also, the [size] of an array must be a constant integer, because arrays can not be created by variables with no number.

Well... I would like to use an array for storing the coefficients of an expression. The user would be able to select an nth degree equation, and then enter n coefficients for the expression. If the user needed a linear function, n would be one; a quadratic would be two, cubic would be three; etc.

I want to use an array so I can call coeff [0], coeff [1] ... coeff [n-1] individually, instead of having to create float coeffa; float coeffb... float coeff n; and create a very messy code. Is there no way to get around it?

Thanks!
I don't think I understand what you mean... Maybe if you post all relevant code?

You can initialize an array without defining its size, the compiler will automatically determine its size.

You are allowed to do the following in C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
//...
//...
struct function {
	float coeff [];
	float constant;
} ax, bx, cx, dx, ex, fx, gx, hx, ix, jx;

int main()
{
     function f1;
     
     cout<<"Which one do you want ?";
     int n;
     cin>>n;
     
     cout<<f1.coeff[n]<<endl;
     
     return 0;
     
}


Does this help?
I just realized you might be talking about dynamic array allocation. In which case you need to do this:

1
2
3
4
5
6
int n;
cin >> n;
int* array = new int[ n ];
// use array here ...
delete [] array;
//remember to free up memory when you are done :)) 


You should probably read the chapter about Array's in this website's documentation section. Alternatively, you could use a vector.
Topic archived. No new replies allowed.