+ 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???
4 Réponses
+ 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*/ {
}
+ 2
yes, i missed it when i was asking this question
+ 1
construstor was a typo, it is in my code XD
0
you're already declared constructor in class definition?