小编Aql*_*jid的帖子

从文件 C++ 读取时无限循环

虽然我检查了while条件中的EOF,但 while 循环运行了无限次。但它仍然运行了无限次。下面是我的代码:

int code;
cin >> code;
std::ifstream fin;

fin.open("Computers.txt");

std::ofstream temp; // contents of path must be copied to a temp file then renamed back to the path file
temp.open("Computers.txt", ios_base::app);


string line;
string eraseLine = to_string(code);
while (  getline(fin, line) && !fin.eof() ) {
    if (line == eraseLine)
    {
        /*int i = 0;
        while (i < 10)
        {*/
            temp << "";
            //i++;
        //}
    }
    if (line != eraseLine) // write all lines to temp other …
Run Code Online (Sandbox Code Playgroud)

c++ getline infinite-loop file-handling

1
推荐指数
1
解决办法
153
查看次数

C++ 意外调用继承类的函数

我做了以下 3 个类:

struct Parent1
{
    virtual void f()
    {
        cout << "\nParent1::f";
    }
};
struct Parent2
{
    virtual void g()
    {
        cout << "\nParent2::g";
    }
    virtual void z()
    {
        cout << "\nParent2::z";
    }
};
struct Child : public Parent1, public Parent2
{
    virtual void h()
    {
        cout << "\nChild::h";
    }
};
Run Code Online (Sandbox Code Playgroud)

在 main 中,当我调用 的函数zParent2,它会调用h子类的函数。为什么会这样?以下是main函数:

int main()
{
    Child obj;
    Parent2 * p2 = (Parent2*)(Parent1*)&obj;
    p2->z();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance pointers multiple-inheritance

1
推荐指数
1
解决办法
56
查看次数