C++ Struct member string array, expected an expression -
my string array giving error "expected expression" , i'm not understanding why:
#include <iostream> #include <string> #include <sstream> using namespace std; typedef struct { string title; string cells[]; } menu; menu mainm, dpe, dps, dpes, tec, rt, wc, mc, mn, cl, imp, dr, df, pd, ts; int main() { mainm.title = "menu principal"; mainm.cells[] = { "dispositivos de entrada", "dispositivos de saida", "dispositivos de entrada e saida" }; }
the error @ mainm.cells[]
severity code description project file line error (active) expected expression arpsis f:\c++\arpsis\arpsis\testes.cpp 16
in c++, line ill-formed:
mainm.cells[] = { "dispositivos de entrada", "dispositivos de saida", "dispositivos de entrada e saida" };
when declare arrays, may use []
signify fact, when isn't part of declaration, , aren't indexing it, shouldn't use []
@ when referring array.
(as jonathan potter points out, struct definition ill-formed:
typedef struct { string title; string cells[]; } menu;
think -- if supposed complete definition of menu structure, taking size sizeof
should possible. if size of cells
has not been determined yet isn't possible yet.
you can use []
without specifying size, when initializing array list of items, because compiler count items you. if aren't doing that, it's not going work.)
changing code to
mainm.cells = { "dispositivos de entrada", "dispositivos de saida", "dispositivos de entrada e saida" };
gives more intelligible error message:
http://coliru.stacked-crooked.com/a/6279ed5446008616
main.cpp:16:17: error: assigning array initializer list
when initializing array, can use braced expressions initialization form, after initialization, can't anymore, need use loop, or memcpy
or something.
if on c++11 standard, , use vector instead of array, can pretty close wanted, this: http://coliru.stacked-crooked.com/a/e7d6964388bf42ac
or if want stick arrays use std::array
, use std::copy
initialize copying, here. http://coliru.stacked-crooked.com/a/3caac3676c94d913
Comments
Post a Comment