Class member function definition problem

Hello there,
This is a piece of testing code I have:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
 
// function return values
enum {OK,FILTER_TOO_LONG};

// class to store properties of the Menu members
class Properties{
  public:
  double GetValues(int i) {return Values[i];} ;
  void SetValues(int i, double NewValues[i]) { Values[i]=NewValues[i];};
  unsigned long GetLength() {return Length;} ;
  void SetLength(unsigned long NewLength) { Length=NewLength;} ;
  bool GetValid() {return Valid;} ;
  bool SetValid(bool NewValid) { Valid=NewValid;} ;
  double InitValues(unsigned long Length) { Values = new double [Length];};
  private:
  double* Values;   // the data values
  unsigned long Length;  // number of data values
  bool Valid;   // true if the data values have been Valid by the user
  
};

class Menu{
  public:
  Menu();
  ~Menu();
  
  private:
  // define the filter and its initial values
  Properties Filter;// = {0,0,false};

  // define the original data and its initial values
  Properties OriginalData;// = {0,0,false};

  // define the filtered data and its initial values
  Properties FilteredData;// = {0,0,false};
};

// Control the principal operations of the program
// Arguments: None
Menu::Menu()
{
	Filter.SetLength(5);
}
int main()
{

}

I am using another class and I want to be able to access private members from this class. That is why I use accessors and mutators. One mutator function is causing me a lot of troubles.
 
void SetValues(int i, double NewValues[i]) { Values[i]=NewValues[i];};

When I compile it error pops out: error C2057: expected constant expression
The members should be private, so are there any solutions how to solve this problem and keep the functionality? I need to be able to access and change each element of the pointer Values using this mutator function.
Thank you for the help!
1
2
3
4
5
6
class A{
  friend class B; //B has access to private members of A
};

void Properties::SetValues(double NewValues[]); //change the entirely array (your class knows the size)
void Properties::SetValues(int index, double NewValue); //change one value 
However your setter/getter don't do anything. It's the same as having the members public.

Edit: SetLength is too dangerous, and InitValues could cause memory leaks. Why don't just use a std::vector?
Last edited on
Topic archived. No new replies allowed.