1

I am newbie in C++. Previously, I learnt the C language and decide to shift to learn C++ language. However, I find something hard for me to follow, particularly in using global variables. I wrote a program and I get wrong. When I tried to debug, my program show error in the line

item* list = ::array[index].head;

in which item is

class item
{
public: 
    int key, data;
    item* next;
};

and array is a global variable which is define as

class arrayitem
{
public:
    item* head;
    item* tail;
};
arrayitem* array;

Edit: This is what I get when I debug my program: enter image description here

Hoang Nam
  • 548
  • 3
  • 18
  • 3
    My advice, remove all mutable globals. Constants are fine, but when you start mutating global state it makes it very hard to reason about the code and can be the source of painful bugs. – NathanOliver Feb 24 '20 at 15:01
  • @NathanOliver however, I need to use global variables to reduce the parameters passed into the function. – Hoang Nam Feb 24 '20 at 15:02
  • 4
    That's doubtful. If you have lots of parameters you encapsulate them into a object. You can also use member functions so that you don't need to pass the object to the function, but just call the function on the object. – NathanOliver Feb 24 '20 at 15:05
  • @NathanOliver That's a good advice. However, as I've mentioned, I'm new in Cpp and OOP (Object-oriented programming). So, you can see that I wrote my program nearly same as C program. First I need to solve this problem to understand the global variables in Cpp – Hoang Nam Feb 24 '20 at 15:08
  • 1
    Please edit your question to provide a [mcve]. How do you initialize `array` etc.? – G.M. Feb 24 '20 at 15:11
  • 2
    Why does being new stop you from doing it "the right way"? If you start off with good habits, you wont have bad ones to break later. It takes a lot more time to unlearn habits then it does to learn the right one the first time. – NathanOliver Feb 24 '20 at 15:12
  • @NathanOliver I know it's good. But my function is ``void`` so I still don't know how to use it in a ``class``. Or I need to change it to another form of function? – Hoang Nam Feb 24 '20 at 15:17
  • Do you happen to have `using namespace std;` in your code? `std::list` exists, and if you try to name variable `list` when `using namespace std;` is in place then error happens. Other than that, I don't see any possible issues with the line you've shown. We would need [mcve] to help you - include full error message you got. – Yksisarvinen Feb 24 '20 at 15:21
  • @Yksisarvinen Of course I did add ``using namespace std;`` and I get this error – Hoang Nam Feb 24 '20 at 15:23
  • 2
    @HoangNam Please see ["Why is “using namespace std;” considered bad practice?"](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – G.M. Feb 24 '20 at 15:26
  • I have updated my question with a photo of debug error. All of you can check it for me to give me some advice to fix it – Hoang Nam Feb 24 '20 at 15:31
  • How do you initialize `array`? The issue is either that `index` is out of bounds for `array` or `array` was not initialized at all. – Yksisarvinen Feb 24 '20 at 15:34
  • @Yksisarvinen I initialize ``array`` by `` arrayitem *array = nullptr;``. Is there anything wrong? – Hoang Nam Feb 24 '20 at 15:36
  • @HoangNam You are dereferencing `array` in `::array[index]`. You cannot dereference a null pointer. That is the same rule as in C. – walnut Feb 24 '20 at 15:39
  • You say you know some C, what would happen if you did `int* a = NULL; printf("%d", a[0]);`? – Yksisarvinen Feb 24 '20 at 15:40
  • @Yksisarvinen I think that it change the value to 0 and has the address into the address of NULL – Hoang Nam Feb 24 '20 at 15:42
  • 1
    @HoangNam Note that in the image shown `array` is clearly *not* null so you must have set it elsewhere. That's why you need to provide a [mcve]. – G.M. Feb 24 '20 at 15:43

0 Answers0