C++ std::ifstream in constructor problem

前端 未结 3 1165
忘了有多久
忘了有多久 2021-01-06 09:50

I\'ve got a problem with this code:

#include 

struct A
{   
    A(std::ifstream input)
    {
        //some actions
    }
};

int main()
{
           


        
3条回答
  •  难免孤独
    2021-01-06 10:21

    Two bugs:

    • ifstream is not copyable (change the constructor parameter to a reference).
    • A(input); is equivalent to A input;. Thus the compiler tries to call the default constructor. Wrap parens around it (A(input));. Or just give it a name A a(input);.

    Also, what's wrong with using a function for this? Only the class's constructor is used it seems, which you seem to abuse as a function returning void.

提交回复
热议问题