c – 错误C2440:’=’:无法从’std :: string []’转换为’std
发布时间:2020-12-16 09:28:21 所属栏目:百科 来源:网络整理
导读:现在这个代码出了什么问题! 标题: #pragma once#include stringusing namespace std;class Menu{public: Menu(string []); ~Menu(void);}; 执行: #include "Menu.h"string _choices[];Menu::Menu(string items[]){ _choices = items;}Menu::~Menu(void){}
现在这个代码出了什么问题!
标题: #pragma once #include <string> using namespace std; class Menu { public: Menu(string []); ~Menu(void); }; 执行: #include "Menu.h" string _choices[]; Menu::Menu(string items[]) { _choices = items; } Menu::~Menu(void) { } 编译器抱怨: error C2440: '=' : cannot convert from 'std::string []' to 'std::string []' There are no conversions to array types,although there are conversions to references or pointers to arrays 没有转换!那是什么呢? 请帮助,只需要传递一个血腥的字符串数组并将其设置为Menu类_choices []属性. 谢谢 解决方法
无法分配数组,无论如何您的数组都没有大小.你可能只想要一个std :: vector:std :: vector< std :: string>.这是一个动态的字符串数组,可以很好地分配.
// Menu.h #include <string> #include <vector> // **Never** use `using namespace` in a header,// and rarely in a source file. class Menu { public: Menu(const std::vector<std::string>& items); // pass by const-reference // do not define and implement an empty // destructor,let the compiler do it }; // Menu.cpp #include "Menu.h" // what's with the global? should this be a member? std::vector<std::string> _choices; Menu::Menu(const std::vector<std::string>& items) { _choices = items; // copies each element } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |