Próbuję wprowadzić wektor ciągów znaków przy użyciu listy inicjalizatora. Ale dostaję dziwne zachowanie. Działa, jeśli w konstruktorze znajduje się więcej niż jeden argument, ale podaje błąd, jeśli jest to jedyny argument. Zapoznaj się z poniższym kodem, aby zrozumiećdziwne zachowanie podczas korzystania z listy inicjalizatora w języku C++
// option.h file
#ifndef __OPTION_H__
#define __OPTION_H__
#include <string>
#include <vector>
namespace CppOptParser {
class Option
{
std::vector<std::string> names;
std::string description;
public:
// constructors
Option(const std::vector<std::string>& names);
Option(const std::vector<std::string>& names, const std::string& description);
// destructor
~Option();
};
} // namespace CppOptParser
#endif /* __OPTION_H__ */
// option.cpp file
#include "option.h"
namespace CppOptParser {
Option::Option(const std::vector<std::string>& names)
{
this->names = names;
}
Option::Option(const std::vector<std::string>& names, const std::string& description)
{
this->names = names;
this->description = description;
}
Option::~Option() {}
} // namespace CppOptParser
// main.cpp file
#include "option.h"
#include <iostream>
using namespace CppOptParser;
int main(int argc, char *argv[])
{
Option *opt = new Option({ "f", "filename"}); // gives error -- error C2440: 'initializing' : cannot convert from 'initializer-list' to 'CppOptParser::Option'
Option *opt1 = new Option({"f", "filename"}, "output file name"); // works fine
std::cin.get();
return 0;
}
Używam visual studio 2013. Proszę o pomoc.
Działa na VS2015 – selbie
@ Czy to jest błąd visual studio? – Sourabh
Czy masz zainstalowane najnowsze dodatki service pack dla VS2013? – selbie