Error C2440: '=': cannot convert from 'std :: string []' to 'std :: string []'

now what's wrong with this code!

Title:

#pragma once
#include <string>
using namespace std;

class Menu
{
public:
    Menu(string []);
    ~Menu(void);

};

Implementation:

#include "Menu.h"

string _choices[];

Menu::Menu(string items[])
{
    _choices = items;
}

Menu::~Menu(void)
{
}

the compiler complains:

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

no conversion! so what does that mean?

please help, you just need to pass the bloody array of strings and set it to the class_choices [] attribute.

thank

+3
source share
2 answers

An array cannot be assigned, and your arrays are dimensionless. Probably, you just want to std::vector: std::vector<std::string>. This is a dynamic array of strings and can be assigned just fine.

// 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 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
}
+7
source

You cannot define an array as string _choices[]which defines an array with an unknown size that is illegal.

string * _choices, ( , ).

, , _choices , ?

0

Source: https://habr.com/ru/post/1749782/


All Articles