What's the difference between these?

After writing this code:

DblArray.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

class DblArray
{
public:
	DblArray();
	void Set (int i, double val);
	double Get (int i) const;
	bool IsSet (int i) const;
private: 
        const int maxcells;
	double indexarray[maxcells];
	int setrecord[maxcells];
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cassert>
#include "DblArray.h"

DblArray::DblArray()
:maxcells(5)
{
	for(int i = maxcells;i>=0;i--)
	{
		setrecord[i]=0;
	}
};

void DblArray::Set (int i, double val)
{
	assert (IsSet(i) == false);
	assert (i<=(maxcells-1));
	indexarray[i] = val;
	setrecord[i] = 1;
};

double DblArray::Get (int i) const
{
	assert (IsSet(i)==true);
	return indexarray[i];
};

bool DblArray::IsSet (int i) const
{
	if (setrecord[i]==0)
	{return false;}
	else
	{return true;}
};


I got the error: 'DblArray::maxcells': is not a type name, static, or enumerator.

After searching around and finding a similar case, I found the following solution:
DblArray.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

const int maxcells = 5;                         //Moved maxcells declaration to here

class DblArray
{
public:
	DblArray();
	void Set (int i, double val);
	double Get (int i) const;
	bool IsSet (int i) const;
private:
                                          // From here
	double indexarray[maxcells];
	int setrecord[maxcells];
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cassert>
#include "DblArray.h"

DblArray::DblArray()
                                        //Removed it from my constructor
{
	for(int i = maxcells;i>=0;i--)
	{
		setrecord[i]=0;
	}
};
void DblArray::Set (int i, double val)
{
	assert (IsSet(i) == false);
	assert (i<=(maxcells-1));
	indexarray[i] = val;
	setrecord[i] = 1;
};

double DblArray::Get (int i) const
{
	assert (IsSet(i)==true);
	return indexarray[i];
};

bool DblArray::IsSet (int i) const
{
	if (setrecord[i]==0)
	{return false;}
	else
	{return true;}
};


And now it works fine. The trouble is, I don't understand why. Can anyone explain the difference?
Last edited on
You can't assign to a const value (which is what you are trying to do every time the constructor gets called).

Either remove the const from maxcells, or make it static, or make it external to the class as in the working example.

No const:
1
2
3
4
5
class DblArray
{
  private:
    int maxcells;
};


Static:
1
2
3
4
5
6
7
class DblArray
{
  private:
    static int maxcells;
};

int DlbArray::maxcells = 5;


Hope this helps.
I couldn't seem to get it to work without a const, or by making it a static. I'm still not quite clear what the problem is. It's a const when I use it external to the class, so what's the difference? And when I use the constant in the constructor, I'm not really using it as anything but a constant that designates the size of the array. Then I'm assigning values within the array of that size, not to the constant itself. (At least that's what I intended.) Why would the compiler forbid that?

Thanks for your time.
Sorry, I missed those lines declaring the "indexarray" and "setrecord" arrays. It should be a "static const".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

class DblArray
{
public:
	DblArray();
	void Set (int i, double val);
	double Get (int i) const;
	bool IsSet (int i) const;
private: 
        static const int maxcells = 5;
	double indexarray[maxcells];
	int setrecord[maxcells];
};


and get rid of line 5 in DblArray.cpp.

Hope this helps.
That works. I guess I need to go read up on static variables. Thanks for your help.
Topic archived. No new replies allowed.