要怎么解决啊#include <iostream>

这是什么原因?要怎么解决啊

img

img

#include 
#include
#include
#include
#include
using namespace std;
struct Dir {
    int level;
    mapkids;
    Dir(int l) : level(l) {};
};
void print_dir(Dir& d) {
    map::iterator it;
    for (it = d.kids.begin(); it != d.kids.end(); it++) {
        for (int i = 0; i < d.level; i++) {
            cout << " ";
        }
        cout << it->first << endl;
        print_dir(it->second);
    }
}
int main() {
    int n;
    while (cin >> n && n != 0) {
        Dir root(0);
        while (n--) {
            string s;
            cin >> s;
            int pos = 0;
            int end = 0;
            Dir* p = &root;
            while (pos < s.size()) {
                end = s.find("\\", pos);
                if (end == string::npos) {
                    end = s.length() + 1;
                }
                string file_name = s.substr(pos, end - pos);
                if (p->kids.find(file_name) == p->kids.end()) {
                    p->kids[file_name] = Dir(p->level + 1);
                }
                pos = end + 1;
                p = &(p->kids[file_name]);
            }
            print_dir(root);
            cout << endl;
        }
    }

}
// 64 位输出请用 printf("%lld")

dir要增加默认构造函数

struct Dir {
    int level;
    map<string, Dir>kids;
    Dir(int l) : level(l) {};
    Dir() {level = 0;}
}

该回答引用GPTᴼᴾᴱᴺᴬᴵ
您的代码中似乎没有语法错误,包含 #include iostream 的代码应该可以成功编译。

但是在代码中,print_dir 函数似乎被错误地放在了 main 函数内部,这可能导致编译错误。请尝试将 print_dir 函数移动到 main 函数之外,或者在 main 函数内部之前声明 print_dir 函数。例如:

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <utility>
using namespace std;

struct Dir {
    int level;
    map<string, Dir> kids;
    Dir(int l) : level(l) {};
};

void print_dir(Dir& d);

void print_dir(Dir& d) {
    map<string, Dir>::iterator it;
    for (it = d.kids.begin(); it != d.kids.end(); it++) {
        for (int i = 0; i < d.level; i++) {
            cout << " ";
        }
        cout << it->first << endl;
        print_dir(it->second);
    }
}

int main() {
    int n;
    while (cin >> n && n != 0) {
        Dir root(0);
        while (n--) {
            string s;
            cin >> s;
            int pos = 0;
            int end = 0;
            Dir* p = &root;
            while (pos < s.size()) {
                end = s.find("\\", pos);
                if (end == string::npos) {
                    end = s.length() + 1;
                }
                string file_name = s.substr(pos, end - pos);
                if (p->kids.find(file_name) == p->kids.end()) {
                    p->kids[file_name] = Dir(p->level + 1);
                }
                pos = end + 1;
                p = &(p->kids[file_name]);
            }
            print_dir(root);
            cout << endl;
        }
    }
    return 0;
}


此外,如果您使用的是在线编译器,那么您需要确保选择正确的编译器选项和头文件,以便您的代码能够成功编译。