visual studio array assignment | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

visual studio array assignment

this code gave me error: //Foo.h class Foo { //something... private: int a, b, c[2]; }; //Foo.cpp Foo::Foo() { int c = {0,0}; } WWHHHYYYY???

24th Jun 2018, 1:05 PM
Ader
Ader - avatar
4 Answers
+ 2
first of all you forgot to declare the constructor in the class definition,declare it in public section of the class: //Foo.h class Foo { //something... public: Foo(); private: int a, b, c[2]; }; second, you cannot assign directly to an array after its declaration same as you do: //Foo.cpp Foo::Foo() { c = {0,0}; } you have to either assign the value at declaration or use loop to assign elements, since your array seems to be a member variable, you can also initialize it in the constructor initialization list like that: Foo::Foo(): c{0} /*every element in c is set to 0*/ { } or Foo::Foo(): c{10,200} /*assign elements*/ { }
24th Jun 2018, 2:58 PM
MO ELomari
+ 2
yes, i missed it when i was asking this question
24th Jun 2018, 3:08 PM
Ader
Ader - avatar
+ 1
construstor was a typo, it is in my code XD
24th Jun 2018, 3:02 PM
Ader
Ader - avatar
0
you're already declared constructor in class definition?
24th Jun 2018, 3:06 PM
MO ELomari