gpt4 book ai didi

Getting Error expected initializer before 'throw' in C++ Exception Handling Problem(在C++异常处理问题中,在“抛出”之前获取错误预期的初始值设定项)

转载 作者:bug小助手 更新时间:2023-10-25 17:34:46 25 4
gpt4 key购买 nike



I am trying to solve the Hacker rank Inherited Code problem and getting the error I cannot solve myself.
I have taken reference from GFG to learn about Exception handling and
bad_exception:
https://www.geeksforgeeks.org/exception-handling-c/
https://www.geeksforgeeks.org/exceptionbad_exception-in-c-with-examples/

我正在尝试解决黑客等级继承代码的问题,并得到了错误,我无法解决自己。我参考了GFG来了解异常处理和BAD_EXCEPTION:https://www.geeksforgeeks.org/exception-handling-c/https://www.geeksforgeeks.org/exceptionbad_exception-in-c-with-examples/


#include <iostream>
#include <string>
#include <exception>
using namespace std;
void BadLengthException(int n) throw(int) throw(bad_exception)
{
if(n){
throw n;
}
}
bool checkUsername(string username) {
bool isValid = true;
int n = username.length();
if(n < 5) {
throw BadLengthException(n);
}
for(int i = 0; i < n-1; i++) {
if(username[i] == 'w' && username[i+1] == 'w') {
isValid = false;
}
}
return isValid;
}

int main() {
int T; cin >> T;
while(T--) {
string username;
cin >> username;
try {
bool isValid = checkUsername(username);
if(isValid) {
cout << "Valid" << '\n';
} else {
cout << "Invalid" << '\n';
}
} catch (BadLengthException e) {
cout << "Too short: " << e.what() << '\n';
}
}
return 0;
}

更多回答

I have taken reference from GFG -- That site is notorious for bad C++ advice, coding examples, and whatever else. -- I am trying to solve the Hacker rank Inherited Code -- It seems you are trying to learn C++ by going to competition websites and geeksforgeeks. Sorry, but to learn one of the most difficult computer languages out there, it requires investing in peer-reviewed books and materials.

我引用了GFG--该站点以糟糕的C++建议、编码示例和其他任何东西而臭名昭著。--我正在尝试解决黑客级别的继承代码--似乎您正在尝试通过访问竞赛网站和极客来学习C++。对不起,要学习这门最难的计算机语言之一,需要投资于同行评议的书籍和材料。

Your code is not legal C++17. This is the stuff you get when you use bad websites to learn C++ programming.

你的代码不是合法的C++17。这是你使用不好的网站学习C++编程时得到的东西。

The dynamic exception specification syntax throw(/*some type*/) has been removed from C++ since C++17 and had been deprecated since C++11. It isn't valid anymore. The code is severely out-dated and won't compile on most modern compilers with default settings. The linked site is known to be full of errors and bad advice and a horrible source to learn C++.

自C++17起,已从C++中删除了动态异常规范语法掷出(/*某些类型*/),并从C++11起弃用了该语法。它不再有效。该代码严重过时,不能在大多数使用默认设置的现代编译器上编译。众所周知,这个链接的站点充满了错误和糟糕的建议,是学习C++的糟糕来源。

Correct way to inherit from std::exception.

从std::Exception继承的正确方式。

What changes can be made in the code to remove the error?

可以在代码中进行哪些更改以消除该错误?

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com