views:

1877

answers:

6

Hello,

I have a c++ class and I am trying to run it in ubuntu:

#ifndef WRONGPARAMETEREXCEPTION_H_

#define WRONGPARAMETEREXCEPTION_H_



#include <iostream>

#include <exception>

#include <string>

using namespace std;



#pragma once



class WrongParameterException: public exception

{

    public:

     WrongParameterException(char* message): exception(message) {};

     virtual ~WrongParameterException() throw() {};

}; 



#endif

when I try to compile it, the compiler gives me this error:

WrongParameterException.h: In constructor ‘WrongParameterException::WrongParameterException(char*)’: WrongParameterException.h:14: error: no matching function for call to ‘std::exception::exception(char*&)’ /usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception() /usr/include/c++/4.3/exception:57: note: std::exception::exception(const std::exception&)

Can anyone tell me what am I doing wrong? I tried changing the message variable to "string" or "const string" or "const string&" but it didnt help..

Here is how I use the new exception that I created from main:

try

    {

     if ((strToInt1 == -1) || (parameters[1] == NULL) || (strToInt3 == -1) || (parameters[3] != NULL))

     {

      throw WrongParameterException("Error in the config or commands file");

     }

    }

    catch(WrongParameterException e)

    {

     log.addMsg(e.what());

    }

Thanks in advance,

Greg

+5  A: 

std::exception does not have a constructor that takes any kind of string, only a virtual what() method that returns the exception description.

You will have to store the string yourself and return it from there.

Timbo
+3  A: 

std::exception's constructor doesn't take a string argument. You're trying to give it one, which is what causes the compile error.

You need to store your string, which would be better to handle as a std::string rather than a raw pointer, and return it from the what() method.

unwind
A: 

Looking at the declaration of the exception class in MS VS2K5, the constructor you want is:

exception (const char *const&);

so try changing your constructor to:

WrongParameterException (const char *const message)

and see if that helps. Otherwise, store the pointer in your own class and implement all the relavent methods.

Skizz

Skizz
A: 

A simple solution is to design your exception diferently. Here is a simple example:

class MyException : public Exception
{
public:
   MyException(CString strError) { m_strError = strError; }

   CString m_strError;
};

Then you can simply use your exception message as you please. This is because Exception does not have a contructor that excepts a String, so you have to stlre it on your own.

Rekreativc
+3  A: 

First, #pragma once is the wrong way to go about it, learn about header include guards. Header Guards explains what they are and how to use them.

Second, you are calling the constructor of std::exception with a parameter it does not know, in this case a pointed to a character array.

#include <stdexcept>
#include <string>

class WrongParameterException : public std::runtime_error {
public:
    WrongParameterException(const string& message) 
        : std::runtime_error(message) { };
};

Would probably be what you want. For more information on exceptions, check out C++ FAQ Lite article on Exceptions and the exceptions article at cplusplus.com.

Good luck!

X-Istence
Header guards are more correct and portable, but if you know the code is to be compiled with the MS compilers, then #pragma once will get you faster compilation (it doesn't have the parse the file again to find the matching #endif).
Michael Kohne
He specified that he is running this on Ubuntu, so I am assuming that is GCC. In which case the PRAGMA statement does not work in the first place.
X-Istence
+2  A: 

My advice would be:

  1. Inherit from std::runtime_error. As advised by X-Istence above. It is conceptually a runtime error, and also the std::runtime_error constructor accepts a std::string as argument describing what happened.
  2. About your catching the exception. I'd use catch(WrongParameterException const& e) (note the const reference) instead of catch(WrongParameterException e), because first, the exception is normally constant in your case, and, also, using the reference, you catch any subclass of WrongParameterException also in case you code evolves with some more refined exception handling.
Diego Sevilla