3

I could not declare an array of strings in my class. Below my class definition:

class myclass{

    public:
        int ima,imb,imc;
        string luci_semaf[2]={"Rosso","Giallo","Verde"};
  };

and my main file

#include <iostream>
#include <fstream> 
#include "string.h"
#include <string>
using namespace std;
#include "mylib.h"
int main() {

    return 0;
}

Why do I get the following warnings / error?

enter image description here

2
  • 2
    C ≠ C++. Also please edit your question to include the error message you are asking about as text instead of a tiny picture of three separate diagnostics. Commented Dec 5, 2015 at 15:46
  • First, value assignment should be in the constructor. Second, if you intend to assign 3 string values to your array then adjust the size to 3 as well. Commented May 14, 2020 at 13:39

4 Answers 4

6

You have two problems: The first is that you can't initialize the array inline like that, you have to use a constructor initializer list. The second problem is that you attempt to initialize an array of two elements with three elements.

To initialize it do e.g.

class myclass{
public:
    int ima,imb,imc;
    std::array<std::string, 3> luci_semaf;
    // Without C++11 support needed for `std::array`, use
    // std::string luci_semaf[3];
    // If the size might change during runtime use `std::vector` instead

    myclass()
        : ima(0), imb(0), imc(0), luci_semaf{{"Rosso","Giallo","Verde"}}
    {}
};
Sign up to request clarification or add additional context in comments.

1 Comment

You can initialize the array inline like that if you use C++11 or later as the message suggests.
5

You can not initialize data member. You can write like this:

class myclass{
   public:
       myclass() {
            luci_semaf[0] = "Rosso";
            luci_semaf[1] = "Giallo";
            luci_semaf[2] = "Verde";
       }
   private:
       int ima,imb,imc;
       string luci_semaf[3];
 };

You can assign the values of the array in the Сonstructor

6 Comments

but one more question is: protected ofstream file;void creadata(string filename){file.open("txtfile/"filename".txt");} this is not working for me give me error16 [Error] expected ')' before 'filename'
write file path without "" please. And I will reply where errors
for eg ? without what ?
Without quotes because you added unnecessary
|
1

You're declaring an array of size 2 but providing 3 strings!

1 Comment

Because you use a feature of C++11, but you don't tell the compiler to use C++11. Add the option suggested in the message.
1

Try storing the elements in vector of strings, in c++ vectors are used more often.

class myclass{

    public:
        int ima,imb,imc;
        std::vector<std::string> strings;
        myclass() {
           strings.push_back("blabla");
        }
  };

3 Comments

i use using namespace and what does mean vector<std::string> strings we have not done this yet
It means you are creating a vector that each element is of type string. Everything that is in <> it is a template argument. Read more about in C++ site about all the std features.
Add #include <vector> to your code to use std::vector.

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.