简体   繁体   English

如何在C ++中修复“没有可行的重载'='”

[英]How to fix “ No viable overloaded '=' ” in C++

This functions purpose is to put the individual lines of a .txt file into an array no bigger than 20. However, I don't know how to progress without being able to assign each line in my loop to that array. 此功能的目的是将.txt文件的各行放入不大于20的数组中。但是,如果无法将循环中的每一行分配给该数组,我不知道如何进行处理。

int read_file(string file_name, person map[20], int 
line_limit)
{
    int line_count = 0;
    string x;
    person specific;

Btw the person class has 2 strings called first_name and last_name and 1 int named age. 顺便说一句,person类有2个名为first_name和last_name的字符串和1个名为age的int。 StackOverflow won't allow me to post the entire program I guess. StackOverflow不允许我发布整个程序。

    fstream input_file;



    input_file.open(file_name, ios::in); 
    if (input_file.is_open())
    {
        cout << "WORKING" << endl;
        //Loop through .txt file
        while (!input_file.eof() && line_count < 
line_limit)
        {
            if (input_file.good())
            {
                input_file >> x;
                map[line_count] = x;   

The ABOVE line derives "No viable overloaded '=' " error. 上面的行产生“没有可行的过载'='”错误。

                line_count++;
            }

        }
     }
    else
        cout << "Not Working" << endl;
        return 1;

    return 0;
}

Any tips would also be great! 任何提示也将很棒!

This is the .txt file I'm looping through. 这是我正在遍历的.txt文件。 I want to assign each firs name, last name, and age as I loop through the .txt file. 我想在遍历.txt文件时分配每个冷杉的名字,姓氏和年龄。 I want to assign it to string x and then put the what is in x at the time into the array that is type person that takes parameters string last_name, string first_name, and int age. 我想将其分配给字符串x,然后将当时x中的内容放入类型为person的数组,该数组采用参数字符串last_name,字符串first_name和int age。

Ann Christensen  70
Carlos Morales   68
David Bowman     45
Frank Bowman     37
John Bowman      30
Kathleen Gueller 34
Mark Bowman      42
Mark Bowman      13
Richard Bowman   47
Susan Cox        36
class person
{



private:
    string first_name;
    string last_name;
    int age;

//Person Constructor - Empty
public:
    person()
    {
        first_name = "";
        last_name = "";
        age = 0;

        //void get(istream &);
        //void put(ostream &);
        //bool operator = ();
}

You're trying to assign a string into a position that should hold a person object. 您正在尝试将字符串分配到应放置person对象的位置。 This attempts to do either 1, or, if that fails, goes on to 2. 这将尝试执行1,或者如果执行失败,则继续执行2。

  1. Convert the string into a person , using a conversion that would be defined by the class person , which looks like operator string() 使用类person定义的转换将字符串转换为person ,类似于operator string()

  2. Assign the string to the person using a method operator=(string) . 使用方法operator=(string)将字符串分配给该person This must be defined in the person class. 这必须在person类中定义。

Another option, since, what I think you're trying to do is to assign to a specific member, is to define a setter method. 因为我认为您要尝试的另一种选择是分配给特定成员,所以定义了一个setter方法。 Usually, this would look like set_first_name(string) , if you are defining a setter for first_name , and would assign first_name to the accepted parameter. 通常,如果您要为first_name定义设置器,则其外观类似于set_first_name(string) ,并将first_name分配给接受的参数。 Basically, you just need some way to set first_name outside the class. 基本上,您只需要某种方法即可在类外设置first_name

If you're assigning a string to another string, you won't get an operator= error. 如果将一个字符串分配给另一个字符串,则不会出现operator=错误。 That only happens with user-defined classes. 这仅在用户定义的类中发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM