0

My string array is giving a error "Expected an Expression" and 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 is at mainm.cells[]

Severity Code Description Project File Line Error (active) expected an expression Arpsis f:\C++\Arpsis\Arpsis\testes.cpp 16

3
  • Maybe use a better compiler? and make sure your warning levels are on the maximum. Commented Jan 16, 2016 at 23:30
  • c++ doesn't let you initialise an array like that. You need to declare its size in advance. Use std::vector<std::string> instead. Commented Jan 16, 2016 at 23:33
  • possible duplicate here Commented Jan 16, 2016 at 23:33

1 Answer 1

3

In C++, this line is ill-formed:

 mainm.cells[] = { "Dispositivos de Entrada", "Dispositivos de Saida", "Dispositivos de Entrada e Saida" };

When you declare arrays, you may use [] to signify the fact, but when it isn't part of a declaration, and you aren't indexing it, you shouldn't use [] at all when referring to the array.

(As Jonathan Potter points out, the struct definition is also ill-formed:

typedef struct {
    string title;
    string cells[];
} menu;

Think about it -- if this is supposed to be a complete definition of the menu structure, then taking its size with sizeof should be possible. But if the size of cells has not been determined yet then it isn't possible yet.

You can use [] without specifying a size, when you are initializing the array with a list of items, because then the compiler will just count the items for you. But if you aren't doing that, it's not going to work.)

Changing your code to

mainm.cells = { "Dispositivos de Entrada", "Dispositivos de Saida", "Dispositivos de Entrada e Saida" };

gives a more intelligible error message:

http://coliru.stacked-crooked.com/a/6279ed5446008616

main.cpp:16:17: error: assigning to an array from an initializer list

When you are initializing an array, you can use the braced expressions initialization form, but after initialization, you can't do that anymore, you need to use a loop, or memcpy or something.

If you are on C++11 standard, and you use a vector instead of an array, you can do something pretty close to what you wanted, like this: http://coliru.stacked-crooked.com/a/e7d6964388bf42ac

Or if you want to stick with arrays you could use std::array and use std::copy to initialize it by copying, like here. http://coliru.stacked-crooked.com/a/3caac3676c94d913

Sign up to request clarification or add additional context in comments.

3 Comments

string cells[]; is also ill-formed.
Well but how do i access the values inside a vector, i never used them :s
same way you access them inside an array... vector is probably the most widely used standard library class, you should learn that one, its really useful :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.