error: expected primary-expression before ';' token

Aug 1, 2013 at 3:31pm
I got the error "error: expected primary-expression before ';' token" when trying to compile the following code. What might be the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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 (std::invalid_argument& ex) {
        cout << "invalid argument : " << ex.what();
    }
    catch (MyException& ex){
    }

    return 0;
}
Aug 1, 2013 at 4:09pm
Try:
throw MyException();
Aug 1, 2013 at 4:15pm
I tried it but then I got another error.

1
2
3
4
5
6
7
8
9
10
11
12
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 
main.cpp:15:5: warning: exception of type ‘MyException’ will be caught [enabled by default]
main.cpp:12:5: warning:    by earlier handler for ‘std::invalid_argument’ [enabled by default]
Aug 1, 2013 at 4:18pm
closed account (Dy7SLyTq)
you need to write a constructor that passes a string to the invalid_argument constructor
Aug 1, 2013 at 4:21pm
I think you need to throw an error object that is an instantiation of your exception class instead of the class name by itself.
Try doing
throw MyException("what argument string");

I tried that and to my surprise it did not work.
I think you may have to type a constructor and have it call the super constructor.
Last edited on Aug 1, 2013 at 4:28pm
Aug 1, 2013 at 4:33pm
By default the default constructor (the constructor that takes no arguments) will call the default constructor of the base classes. std::invalid_argument has no default constructor so MyException will not get a default constructor. If you want MyException to have a default constructor you will have to define it yourself, and specify what std::invalid_argument constructor you want to use in the constructor initialization list.

 
MyException::MyException() : std::invalid_argument("what argument string") {}
Last edited on Aug 1, 2013 at 4:35pm
Aug 1, 2013 at 4:58pm
Thanks Peter87, you explained it well.
Aug 1, 2013 at 5:06pm
closed account (Dy7SLyTq)
I tried that and to my surprise it did not work.
I think you may have to type a constructor and have it call the super constructor.


of course it doesnt work. in c++ classes dont have default constructors that take args. so doing that without writing a constructor that takes a string will flag an error.
Topic archived. No new replies allowed.