VSC13 - 无法为数组/ STRINGS指定显式初始值设定项

时间:2015-06-02 06:29:05

标签: arrays string visual-c++ visual-studio-2013 initialization

据我所知,Microsoft Visual Studios Community 2013的数组初始化存在问题,但我如何针对字符串专门解决这个问题呢?请尽量解释答案,我对此仍然很陌生。

class a{
public:
    string words[3] = {"cake","pie","steak"};
};

2 个答案:

答案 0 :(得分:2)

正如您所写,它不会编译,因为您无法在定义中初始化非静态数组。这虽然有效:

#include <array>
class a{
public:
    a() : words({"cake","pie","steak"})
    {
    }

    std::array<std::string, 3> words;
};

答案 1 :(得分:1)

你在找这样的东西吗?

public class BartServer {

    public static void main(String[] args){
    int port = 5555;

    BartQuote bart = new BartQuote();

    try
    {
        System.out.println("BartServer 1.0");
        System.out.println("Listening on port " + port);
        ServerSocket ss = new ServerSocket();
        ss = new ServerSocket(port);
        Socket s;
        s = ss.accept();

        String client;
        client = s.getInetAddress().toString();
        System.out.println("Connected to" + client);

        Scanner in = new Scanner(s.getInputStream());
        PrintWriter out;
        out = new PrintWriter(s.getOutputStream(), true);

        out.println("Welcome to BartServer 1.0");
        out.println("Enter GET to get a quote or BYE to exit.");

        while(true)
        {
            String input = in.nextLine();
            if(input.equalsIgnoreCase("BYE"))
                break;
            else if(input.equalsIgnoreCase("GET"))
            {
                out.println(bart.getQuote());
                System.out.println("Serving " + client);
            }
            else
                out.println("Huh?");
        }
        out.println("So long, suckers!");
        s.close();
        System.out.println("Closed connection to " + client);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    }
}