Array in class

I have an array in my class but i cant figure out how to use it in my program, i fixed some issues with ti before but still cant get it to work

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
#include <iostream>
#include <string>

using namespace std;

class Variables
{
    public:
        Variables()
        {
            Items[10];
        }

    private:
        int Items[];
};

int main()
{
    Variables VO;

    string plrname;

    cout << "What is your name?" << endl;
    cin >> plrname;

    cout << "enter stuff" << endl;
    cin >> Items[0];

    cout << Items[0];
}




C:\Users\Chay Hawk\Desktop\Shipping Supplies\main.cpp||In constructor 'Variables::Variables()':|
C:\Users\Chay Hawk\Desktop\Shipping Supplies\main.cpp|11|warning: statement has no effect|
C:\Users\Chay Hawk\Desktop\Shipping Supplies\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Shipping Supplies\main.cpp|28|error: 'Items' was not declared in this scope|
||=== Build finished: 1 errors, 1 warnings ===|
Last edited on
The following lines in your code do nothing at all:
Items[10]; VO.Items[];


Items is a member variable INSIDE an object of type Variables. To use it, you have to go via a class of that object.

1
2
3
Variables VO;
...
cin >> Items[0];  // Bad. No such variable 

Compare with:
1
2
3
Variables VO;
...
cin >> VO.Items[0];  // Good.  



If you want to be able ti directly access member variables, you'll have to make them public.


Here is your code rewritten. Please compare it to your original code and ask about anything you don't understand.

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
#include <iostream>
#include <string>

using namespace std;

class Variables
{
    public:
        Variables()
        {
            Items = new int[10];
        }

~Variables()
        {
            delete Items;
        }
  
        int* Items;
};

int main()
{
    Variables VO;
    
    string plrname;

    cout << "What is your name?" << endl;
    cin >> plrname;

    cout << "enter stuff" << endl;
    cin >> VO.Items[0];

    cout << VO.Items[0];
}
closed account (o3hC5Di1)
Hi there,

First off, Items[] is private, which means you can never just access it outside a class. If you want to access it, you'l need to write accessor/mutator functions (getters/setters) to do so.

Second, even if it was made public, you would access it as such: VO.Items[0];. The array is part of an object, so you need to tell that to C++.

Also, I'm not sure whether that's a valid way to declare and define the array - I think you need to do

1
2
private:
        int Items[10];

Hope that helps.

All the best,
NwN


Ok what is there a new Variables class and what is the tilde symbol do? Also what is delete. Actually everything in the class you changed needs explaining as ive seen it all before but not sure what it does.
Last edited on
closed account (o3hC5Di1)
Hi there,

The example Mr. Moschops gave uses dynamic memory: http://cplusplus.com/doc/tutorial/dynamic/
When using dynamic memory, it's important to free the allocated memory space, which is done in the destructor (~Variables()). Destructors are called automatically when an object is destroyed, as opposed to a constructor which is called upon creation.

As I said, Items[] is within an object, so you will have to tell C++ to look for the variable in the object, by using the dot operator. VO.Items[0]; can be read as: value 0 from the Items array which is a member of the VO object.

Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.