这是我的代码:
#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
我需要更改什么?
数组在C ++中不可复制。所以这永远都行不通
Arguments(const string(&arg)[N]) : arguments{ arg } {}
您可以std::array<std::string, N>
改用它,也可以在构造函数的主体中逐元素复制数组元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句