首页 > 解决方案 > 错误 c++ 没有可行的重载 '=' ,我的 operator= 有问题

问题描述

嗨,我的 operator= 在 main.cpp 中有这个错误,我不知道它有什么问题。或者问题可能出在功能上……我不知道。如果你能帮助我,那就太好了!谢谢!

我的 main.cpp:

#include <iostream>
#include "List.h"
using namespace std;

List merge(List &lst1, List &lst2);
void makeSet(List lst);

int main()
{
    List lst1, lst2, mergedList;

    cout<<"enter sorted values for the first list:"<< endl;
    cin>>lst1;
    cout<<"enter sorted values for the second list:"<< endl;
    cin>>lst2;

    mergedList = merge(lst1,lst2); //My ERROR is here (No viable overloaded '=')
    cout <<"the new merged list: " << mergedList <<endl;
    makeSet(mergedList);
    cout<<"the new merged set: " << mergedList << endl;

    return 0;
}

我的操作员=(在 List.cpp 中):

List& List::operator=(List& l){
    l.clear();
    Link *src, *trg;

        head = new Link(l.head->value, NULL);
        src = l.head;
        trg = head;
        while(src->next!=NULL)
        {
            //create new Link, and attach to end of new list
            trg->next = new Link((src->next)->value, NULL);
            src = src->next;
            trg = trg->next;
        }
    return *this;
    }

标题中的我的运算符:

List& operator=(List&);

标签: c++classlinked-listoperator-overloading

解决方案


推荐阅读