Why do i get “invalid use of non-static data member” error when i try to create an array inside class? [duplicate]Array initialization use const variable in C++error: request for member '..' in '..' which is of non-class typestatic constructors in C++? I need to initialize private static objectsDeclaring a non static const array as class memberStatic constant string (class member)C++11 allows in-class initialization of non-static and non-const members. What changed?invalid use of non-static data member (array)error: invalid in-class initialization of static data member of non-integral type 'const char[]'Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInvalid use of non static data memberStatic data member initialization in the class definition?

What do you call the action of someone tackling a stronger person?

How often can a PC check with passive perception during a combat turn?

First-year PhD giving a talk among well-established researchers in the field

Could Sauron have read Tom Bombadil's mind if Tom had held the Palantir?

What is this particular type of chord progression, common in classical music, called?

When is it ok to add filler to a story?

Symbolic equivalent of chmod 400

Should I hide continue button until tasks are completed?

Why aren't (poly-)cotton tents more popular?

How can I set command-line parameters through `.emacs` file?

Why cruise at 7000' in an A319?

Is there a short way to compare many values mutually at same time without using multiple 'and's?

How to determine what is the correct level of detail when modelling?

How come I was asked by a CBP officer why I was in the US?

Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback

Is this one of the engines from the 9/11 aircraft?

What happens when your group is victim of a surprise attack but you can't be surprised?

Fedora boot screen shows both Fedora logo and Lenovo logo. Why and How?

Do French speakers not use the subjunctive informally?

Why does the A-4 Skyhawk sit nose-up when on ground?

Why isn’t the tax system continuous rather than bracketed?

Should I include salary information on my CV?

Analog is Obtuse!

Ending: accusative or not?



Why do i get “invalid use of non-static data member” error when i try to create an array inside class? [duplicate]


Array initialization use const variable in C++error: request for member '..' in '..' which is of non-class typestatic constructors in C++? I need to initialize private static objectsDeclaring a non static const array as class memberStatic constant string (class member)C++11 allows in-class initialization of non-static and non-const members. What changed?invalid use of non-static data member (array)error: invalid in-class initialization of static data member of non-integral type 'const char[]'Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInvalid use of non static data memberStatic data member initialization in the class definition?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2
















This question already has an answer here:



  • Array initialization use const variable in C++

    4 answers



I created an array with the length of x but I get the error




invalid use of non-static data member Test::x.




I tried to replace int newArray[x]; with int newArray = new int[x]; but still didn't work out.
When I declared the newArray[x] in the constructor or put static const before int x = 10, the code run successfully.
Why is that?



#include <iostream>
#include <vector>
using namespace std;

class Test

private:
int x = 10;
int newArray[x];
public:
Test();
~Test();
;
int main()


return 0;










share|improve this question















marked as duplicate by Alan Birtles, P.W c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 11:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • You may want to read this : stackoverflow.com/questions/18996258/…

    – Spinkoo
    Mar 25 at 11:19











  • The size of arrays must be known at compile-time. Use vector if that cannot happen.

    – M.M
    Mar 25 at 11:21












  • Don't edit question in a way to invalidate answers please

    – Ayxan
    Mar 25 at 11:35


















-2
















This question already has an answer here:



  • Array initialization use const variable in C++

    4 answers



I created an array with the length of x but I get the error




invalid use of non-static data member Test::x.




I tried to replace int newArray[x]; with int newArray = new int[x]; but still didn't work out.
When I declared the newArray[x] in the constructor or put static const before int x = 10, the code run successfully.
Why is that?



#include <iostream>
#include <vector>
using namespace std;

class Test

private:
int x = 10;
int newArray[x];
public:
Test();
~Test();
;
int main()


return 0;










share|improve this question















marked as duplicate by Alan Birtles, P.W c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 11:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • You may want to read this : stackoverflow.com/questions/18996258/…

    – Spinkoo
    Mar 25 at 11:19











  • The size of arrays must be known at compile-time. Use vector if that cannot happen.

    – M.M
    Mar 25 at 11:21












  • Don't edit question in a way to invalidate answers please

    – Ayxan
    Mar 25 at 11:35














-2












-2








-2









This question already has an answer here:



  • Array initialization use const variable in C++

    4 answers



I created an array with the length of x but I get the error




invalid use of non-static data member Test::x.




I tried to replace int newArray[x]; with int newArray = new int[x]; but still didn't work out.
When I declared the newArray[x] in the constructor or put static const before int x = 10, the code run successfully.
Why is that?



#include <iostream>
#include <vector>
using namespace std;

class Test

private:
int x = 10;
int newArray[x];
public:
Test();
~Test();
;
int main()


return 0;










share|improve this question

















This question already has an answer here:



  • Array initialization use const variable in C++

    4 answers



I created an array with the length of x but I get the error




invalid use of non-static data member Test::x.




I tried to replace int newArray[x]; with int newArray = new int[x]; but still didn't work out.
When I declared the newArray[x] in the constructor or put static const before int x = 10, the code run successfully.
Why is that?



#include <iostream>
#include <vector>
using namespace std;

class Test

private:
int x = 10;
int newArray[x];
public:
Test();
~Test();
;
int main()


return 0;





This question already has an answer here:



  • Array initialization use const variable in C++

    4 answers







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 11:35









Ayxan

3,4147 silver badges25 bronze badges




3,4147 silver badges25 bronze badges










asked Mar 25 at 11:16









Minh MốcMinh Mốc

12 bronze badges




12 bronze badges




marked as duplicate by Alan Birtles, P.W c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 11:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Alan Birtles, P.W c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 11:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You may want to read this : stackoverflow.com/questions/18996258/…

    – Spinkoo
    Mar 25 at 11:19











  • The size of arrays must be known at compile-time. Use vector if that cannot happen.

    – M.M
    Mar 25 at 11:21












  • Don't edit question in a way to invalidate answers please

    – Ayxan
    Mar 25 at 11:35


















  • You may want to read this : stackoverflow.com/questions/18996258/…

    – Spinkoo
    Mar 25 at 11:19











  • The size of arrays must be known at compile-time. Use vector if that cannot happen.

    – M.M
    Mar 25 at 11:21












  • Don't edit question in a way to invalidate answers please

    – Ayxan
    Mar 25 at 11:35

















You may want to read this : stackoverflow.com/questions/18996258/…

– Spinkoo
Mar 25 at 11:19





You may want to read this : stackoverflow.com/questions/18996258/…

– Spinkoo
Mar 25 at 11:19













The size of arrays must be known at compile-time. Use vector if that cannot happen.

– M.M
Mar 25 at 11:21






The size of arrays must be known at compile-time. Use vector if that cannot happen.

– M.M
Mar 25 at 11:21














Don't edit question in a way to invalidate answers please

– Ayxan
Mar 25 at 11:35






Don't edit question in a way to invalidate answers please

– Ayxan
Mar 25 at 11:35













1 Answer
1






active

oldest

votes


















1














int newArray[x]; won't work because size of a static array needs to be known at compile time. Adding static constexpr to the declaration of x makes it a compile time constant and that's why the code compiles.
int newArray = new int[x]; won't work either, because operator new returns a pointer, which cannot be assigned to an integer. Having said that, consider using std::vector instead.






share|improve this answer

























  • @Downvoter Can anyone suggest improvements or corrections to this?

    – Ayxan
    Mar 25 at 11:48



















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














int newArray[x]; won't work because size of a static array needs to be known at compile time. Adding static constexpr to the declaration of x makes it a compile time constant and that's why the code compiles.
int newArray = new int[x]; won't work either, because operator new returns a pointer, which cannot be assigned to an integer. Having said that, consider using std::vector instead.






share|improve this answer

























  • @Downvoter Can anyone suggest improvements or corrections to this?

    – Ayxan
    Mar 25 at 11:48















1














int newArray[x]; won't work because size of a static array needs to be known at compile time. Adding static constexpr to the declaration of x makes it a compile time constant and that's why the code compiles.
int newArray = new int[x]; won't work either, because operator new returns a pointer, which cannot be assigned to an integer. Having said that, consider using std::vector instead.






share|improve this answer

























  • @Downvoter Can anyone suggest improvements or corrections to this?

    – Ayxan
    Mar 25 at 11:48













1












1








1







int newArray[x]; won't work because size of a static array needs to be known at compile time. Adding static constexpr to the declaration of x makes it a compile time constant and that's why the code compiles.
int newArray = new int[x]; won't work either, because operator new returns a pointer, which cannot be assigned to an integer. Having said that, consider using std::vector instead.






share|improve this answer















int newArray[x]; won't work because size of a static array needs to be known at compile time. Adding static constexpr to the declaration of x makes it a compile time constant and that's why the code compiles.
int newArray = new int[x]; won't work either, because operator new returns a pointer, which cannot be assigned to an integer. Having said that, consider using std::vector instead.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 11:44

























answered Mar 25 at 11:23









AyxanAyxan

3,4147 silver badges25 bronze badges




3,4147 silver badges25 bronze badges












  • @Downvoter Can anyone suggest improvements or corrections to this?

    – Ayxan
    Mar 25 at 11:48

















  • @Downvoter Can anyone suggest improvements or corrections to this?

    – Ayxan
    Mar 25 at 11:48
















@Downvoter Can anyone suggest improvements or corrections to this?

– Ayxan
Mar 25 at 11:48





@Downvoter Can anyone suggest improvements or corrections to this?

– Ayxan
Mar 25 at 11:48





Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript