0
votes

I'm having an issue with VisualStudio c++ compiler. While making a struct:

struct element{
element* next=NULL;
element* prev=NULL;
char value;
};

the compiler shows an error

main.cpp(21) : error C2864: 'element::next' : only static const integral data members can be initialized within a class

main.cpp(22) : error C2864: 'element::prev' : only static const integral data members can be initialized within a class

On LLVM everything runs fine. How can I fix the issue?

3
What version of Visual Studio are you using? Is it in C++11/14/17 mode?Stephen Newell
I'm sending it on my online academic compiler. All I know is that it is working on VS compiler. I'm not able to check its version :/SuperMario
Perhaps this topic from MSDN can help out? msdn.microsoft.com/en-us/library/acxkb76w.aspxRann Lifshitz
Topic on MSDN shows the problem with static integers. Mine are not static.SuperMario
I would just add a constructor for element.drescherjm

3 Answers

0
votes

Unless you can change the version of the c++ standard being used msvc is going to use the older behavior of not being able to use inline initialization, you will need a default ctor. And because of MSVC debug build behavior you will likely need to explicitly initialize those members to null. (In debug builds MSVC default initializes many items to non-zero values).

struct element{
  element* next;
  element* prev;

  element{()
    : next(), prev()
  {}
};
0
votes

If the compiler version accepts it, you can use the initializer list within a constructor instead.

element():next(NULL),prev(NULL) {};

Your code in this case:

struct element{

    element* next;
    element* prev;

    element():next(NULL),prev(NULL) {};

int value;
}; 

also you can try using nullptr instead of NULL.

0
votes

It seems you use an obsolete C++ compiler. Use one newer: for g++ and clang++ specify std=c++14 or std=c++17. For Visual C++ specify /std:c++14 or /std:c++latest. I think you can set this compiler option for your online academic compiler.

Also avoid using NULL. Use nullptr instead.