【问题标题】:I get "no match for 'operator=' when assigning value to a struct array iin C++在 C++ 中为结构数组赋值时,我得到“'operator=' 不匹配”
【发布时间】:2018-05-23 23:32:35
【问题描述】:

我正在尝试从名为 AGENDA 的文件中读取信息并将其放入结构类型 Persona* 中,该结构类型显示在代码之后。

char arrk [120];
string dato;
arch_g.open("AGENDA.TDAT",ios::in);
//Persona* buffer;
do
{
    for (int k=0; k<tamano; k++)
    {
        arch_g.getline (arrk, 120, '\t');
        string dato (arrk);
        arreglo->nombre=dato;
        arreglo->apellido_p=dato;
        arreglo->apellido_m=dato;
        arreglo->dom.calle=dato;
        int dato1 = (int)arrk;
        arreglo->dom.numero=dato1;
        arreglo->dom.interior=dato;
        arreglo->dom.colonia=dato;
        arreglo->dom.ciudad =dato;
        arreglo->dom.estado=dato;
        dato1 = (int)arrk;
        arreglo->dom.codigo_postal=dato1;
        arreglo->tel.local=dato;
        arreglo->tel.clave_cd=dato;

        per[k] = arreglo;
    }

}

这是人设

struct Persona

{
bool isInitialized;
std::string nombre;
std::string apellido_p;
std::string apellido_m;

Domicilio dom;
Telefono tel;

Persona() : isInitialized(false) {}

}
struct Domicilio
{
bool isInitialized;
std::string calle;
int numero;
std::string interior;
std::string colonia;
std::string ciudad;
std::string estado;
int codigo_postal;

Domicilio() : isInitialized(false) {}
};

struct Telefono
{
bool   isInitialized;
std::string    local;
std::string clave_cd;

Telefono() : isInitialized(false) {}
};

我得到的错误是这样的:

错误:“operator=”不匹配(操作数类型为“Persona”和“Persona*”)|

错误被标记在'per[k]=arreglo; ' 这部分是指 Persona 结构体

注意:候选人是:| 注意:Persona& Persona::operator=(const Persona&)| 注意:没有已知的参数 1 从 'Persona*' 到 'const Persona&' 的转换|

per 和 arreglo 都被声明为全局 Persona*

【问题讨论】:

标签: c++ arrays struct operator-keyword


【解决方案1】:

这是一个指针。取值。或者每个人都必须是一个角色**

per[k] = *arreglo;

如果结构不重要,您可能还需要实现运算符

Persona& operator=(const Persona& o) { ... }

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 2016-01-16
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多