【发布时间】:2020-05-19 16:56:30
【问题描述】:
这是我的代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template<int N>
class Arguments
{
protected:
string arguments[N];
public:
Arguments(const string(&arg)[N]) : arguments{ arg } {}
string operator[](const int &i) const { return arguments[i]; }
};
int main()
{
string arr[3] = { "arg1", "arg2", "arg3" };
Arguments<3> args(arr);
cout << args[2];
return 0;
}
这是我得到的错误:
Severity Code Description Project File Line Suppression State
Error C2440 'initializing': cannot convert from 'const std::string [3]' to 'std::string'
coursework_draft C:\dev\coursework_draft\coursework_draft\coursework_draft.cpp 13
我需要改变什么?
【问题讨论】:
-
你不能用另一个数组初始化一个数组,只能按元素初始化。要么使用循环复制元素,要么使用
std::array,可以正常复制。 -
我修好了。谢谢!
标签: c++ string type-conversion