I want to have an array whose values be declared from main to a class. Here's the sample code.
class test{
public:
const double arr[];
};
int main(){
test t;
t.arr[] = {1, 2};
return 0;
}
When I try to intialize in the main it gives me an error error:unexpected expression.
But if I remove the t.arr[]
in main, it compiles fine.
const double arr[];
- variable length array? Invalid in C++ . t.arr[]
- invalid syntax ( operator[]
call with no arguments?), arr
is also const
and you can't assign to any arrays. But you can do aggregate intialization :
class test {
public:
const double arr[2]; // fixed size
};
int main() {
test t = {{1, 2}}; // not an assignment
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.