views:

156

answers:

3

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&)
+2  A: 

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++?)

derobert
try changing it from a heap object to a stack object and see if it still works.
Lodle
+6  A: 

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.

Johannes Schaub - litb
This is for linux, not vis but ill give your suggestion a try
Lodle
no. the reason it worked in windows is because vis (according to that guys comment) had a bug that made your code valid. gcc in linux behaves correctly in that it rejects it. this is only a theory because i don't know what ERR_BADCURLHANDLE is in your code. but this seem to be likely.
Johannes Schaub - litb
wasn't the ERR_BADCURLHANDLE as can be seen by your latest edit. It's because you throw the gcException(...), which will construct a temporary that is then thrown internally. but that requires a copy constructor that accepts temporaries. the reason why it would work on msvc++ didn't change though.
Johannes Schaub - litb
j_random_hacker
j_random_hacker. he omitted to show it (and some other ctors). but the error message clearly shows he has other constructors in the hood. there aren't much other possibilities for this errors. two: first he used gcc (so, a linker bug), secondly he had to fix his copy constructor.
Johannes Schaub - litb
note that a ctor taking its class-type by reference-to-nonconst is also a copy constructor.
Johannes Schaub - litb
A: 

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

Lodle
Yeah, that's what I said. Mainly because I did that by mistake when compiling my test case :-D
derobert