Question

For my exception class i have a constructor that has multi arguments (...) which works fine under windows, how ever, under linux it compiles fine but refuses to link to it.

Why does this not work under linux?

here is an example:

class gcException
{
public:
    gcException()
    {
        //code here
    }

    gcException(uint32 errId, const char* format = NULL, ...)
    {
        //code here
    }
}


enum
{
    ERR_BADCURLHANDLE,
};

.

Edit

So when i call it like so:

if(!m_pCurlHandle)
    throw gcException(ERR_BADCURLHANDLE);

I get this compile error:

error: no matching function for call to ‘gcException::gcException(gcException)’
candidates are: gcException::gcException(const gcException*)
                 gcException::gcException(gcException*)
                 gcException::gcException(gcException&)
Was it helpful?

Solution

It compiles and links just fine. I expanded your test code to a full "program":

class gcException {
    public:
        gcException() { }
        gcException(int errId, const char* format, ...) { }
};
int main() { new gcException(1, "foo", "bar", "baz"); }

And then g++ -Wall test.cpp ran without errors. According to g++ -v, I have gcc version 4.3.2 (Debian 4.3.2-1.1). Does my quick example compile for you?

(Did you maybe accidentally compile — or link — with gcc instead of g++?)

OTHER TIPS

The problem is that your copy constructor doesn't accept the temporary that you give the throw. It's a temporary and thus an rvalue. A reference-to-nonconst, namely gcException& can't bind to it. Read here on the details.

As a comment on that answer suggests, the microsoft compiler had a bug that made it bind references that point to non-const objects accept rvalues. You should change your copy-constructor to this:

gcException(gcException const& other) {
    // ...
}

To make it work. It says the bug was fixed in Visual C++ 2005. So you would get the same problem with that version onwards. So better fix that problem right away.

Well just figured it out, seems code block was using gcc instead of g++ to compile the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top