当类的构造函数调用其成员函数时,会发生使用已删除函数的错误

Use of deleted function error occurs when the constructor of a class calls its member function?

本文关键字:函数 错误 删除 函数调用 成员      更新时间:2023-10-16

构造函数有一些工作要做,所以我在构造函数内部调用一个成员函数:

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    class Token{
    public:
        Token() {}
        Token(const string &targetfile);
        void GetToken();
        ifstream in;
    };
    Token::Token(const string &targetfile)
    {
        in.open(targetfile);    
        GetToken();
    }
    void Token::GetToken()
    {
        in.close();
    }
    int main(int argc,char *argv[])
    {
        Token first = Token(string(argv[1]));
        return 0;
    }

我得到了这样的错误:

删除函数' Token::Token(Token&&) '的使用注意:' Token::Token(Token&&) '被隐式删除,因为默认定义格式错误

Token first(string(argv[1]));代替Token first = Token(string(argv[1]));