[Solved] Can’t assign a value to a variable inside a class [closed]


This syntax in invalid. You cannot simply write code inside a class definition. Instead, you can write a function and your code will work inside it:

class Foo {
public:
  void bar() {
    int * p = new int;
    *p = 5;
  }
};

int main(int argc, char ** argv) {
  Foo f;
  f.bar();
}

Note: You should prefer to allocate and initialize your variable in a single statement when possible:

int *p = new int(5);

Instead of:

int * p = new int;
*p = 5;

4

solved Can’t assign a value to a variable inside a class [closed]