编译错误:ISO C++ forbids declaration of *** with no type

转自:http://blog.hiwgy.com/2011/07/16/compile_error/

今天遇到个问题,“ISO C++ forbids declaration of *** with no type”。
错误的原因一般是:两个头文件相互include,一般可以采用#ifndef或前置声明解决该问题;
具体用法很简单,就不讲了。

我反复检查,上面的方法并没有解决该编译error。最后,我发现了cpp文件中有“#ifndef __FILENAME_HEADER”,这就是问题的所在了。
由于我是vim新建的.h文件,然后把.h文件copy为.cpp文件,忘记了把vim自动生成的__FILENAME_HEADER去掉,因此发生了几个warning,最终导致了“ISO C++ forbids declaration of *** with no type”。

结论是:编译时,不要盯着error解决error,有的error是warning导致的;要把warning消灭掉。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
抱歉,我在之前的回答中出现了错误。上面的代码中确实存在一个问题,即在检查是否存在相同分数的学生时,需要使用正确的类型`Student`来声明变量。 以下是修正后的代码: ```cpp #include <iostream> #include <vector> #include <algorithm> // 定义学生结构体 struct Student { std::string name; int score; // 重载小于运算符,用于排序 bool operator<(const Student& other) const { if (score == other.score) { return name < other.name; } return score > other.score; } }; int main() { int n; std::cin >> n; std::vector<Student> students; for (int i = 0; i < n; i++) { std::string name; int score; std::cin.ignore(); // 忽略上一行的换行符 std::getline(std::cin, name); std::cin >> score; // 检查是否已存在相同分数的学生 bool exists = false; for (const auto& student : students) { if (student.score == score) { exists = true; break; } } // 若不存在相同分数的学生,则将该学生添加到列表中 if (!exists) { students.push_back({name, score}); } } // 按照要求进行排序 std::sort(students.begin(), students.end()); // 输出结果 for (const auto& student : students) { std::cout << student.name << std::endl; } return 0; } ``` 请将以上代码保存为一个.cpp文件,然后使用C++编译器进行编译运行。输入学生信息后,程序将按照要求进行排序并输出结果。如果你仍然遇到相同的错误,请提供具体的错误信息以供进一步的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值