gpt4 book ai didi

c++ - 错误 : expected primary-expression before ';' token

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:06:05 24 4
gpt4 key购买 nike

我得到了错误

error: expected primary-expression before ';' token

当我尝试编译以下代码时。问题是什么?

#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;

class MyException : public std::invalid_argument{};

int main() {
try {
throw MyException; //here is the problem
}
catch (...){
}

return 0;
}

我也试过这段代码

#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;

class MyException : public std::invalid_argument{};

int main() {
try {
throw MyException(); //here is the problem
}
catch (...){
}

return 0;
}

但是我又遇到了一个错误

main.cpp: In constructor ‘MyException::MyException()’:
main.cpp:6:7: error: no matching function for call to ‘std::invalid_argument::invalid_argument()’
main.cpp:6:7: note: candidates are:
In file included from main.cpp:2:0:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:86:14: note: std::invalid_argument::invalid_argument(const string&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:86:14: note: candidate expects 1 argument, 0 provided
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:83:9: note: std::invalid_argument::invalid_argument(const std::invalid_argument&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:83:9: note: candidate expects 1 argument, 0 provided
main.cpp: In function ‘int main()’:
main.cpp:10:27: note: synthesized method ‘MyException::MyException()’ first required here

最佳答案

你需要创建一个实际的对象来抛出:

throw MyException();

没有括号 MyException 只是类型,它不会创建对象。

关于c++ - 错误 : expected primary-expression before ';' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17999422/

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