xcode - "attempt to use a deleted function" - what does that mean? - c++

I am writing a C++ library in Xcode 4.2.
One of my classes won't compile with this error:
attempt to use a deleted function
There is no specific indication what function it's talking about.
I don't want to post the class code here, but does anybody have any idea what this error means?

I had a similar message with threads (C++11). It turned out that I was passing the wrong number of parameters to the function called by the thread so the thread did not find any function suitable and gave that message.

To add to Carlos' answer, I had the right number of arguments but one of the arguments was being passed by reference. Adding ref() around the variable fixed it for me. See here.

In C++11 you can declare functions as deleted:
struct Foo {
Foo(const Foo &) = delete;
};
Attempting to use such a function is an error. The purpose of doing this is so that, in this example, copy construction of this type is not possible. This is a more direct replacement for the non-copyable trick used pre-C++11.
Also, there are rules in the C++ spec that lead to member functions being implicitly deleted.
The error is telling you that your program attempts to use a deleted function. You'll have to post the error you're getting for more detailed help.

For me It solved it when I passed "this" pointer as a parameter to the function.

For me, the issue was that one of the arguments was a pointer, and I passed NULL directly as an argument. To solve this, I simply created a new NULL pointer which I passed to the function as an l-value instead.

Related

Deducing template type of object while this object is being destroyed

Situation: I have a deque declared as deque<intmax_t> tab and i want to destroy it with deduced template argument. I tried tab.~deque<__decltype(tab.at(0))>() which gives me the compiler error:
Csrt.cpp: In function 'int main(int32_t, char**)':
Csrt.cpp:303:34: the type being destroyed is 'std::deque<long long int>',
but the destructor refers to 'std::deque<long long int&>'
tab.~deque<__decltype(tab.at(0))>();
^
The question is: is something like that doable?
What I know I do not want to do: create dummy variables. Like: auto dummy = tab.at(0)
What I hope is doable: one-line destructor.
What i tried, too: tab.~deque<__decltype(tab[0])>() with result: same as above.
I think that is all information needed. Thank you in advance.
The problem is that the at member function in deque is defined like this:
intmax_t &at(size_t pos);
So your decltype(x.at(0)) actually resolves to intmax_t&.
You can use the remove_reference type trait:
#include <type_traits>
x.~deque<std::remove_reference<decltype(x.at(0))>::type>();
But it is easier if you use the helpful member typedefs available in every standard container:
x.~deque<decltype(x)::value_type>();
Naturally, for this particular case, I think that you can just use the injected class name for the destructor:
x.~deque();
You're calling the destructor on a stack-based object, so I take it you're interested in using placement new later on, similar to this SO post C++ stack allocated object, explicit destructor call.
Using g++ 4.4.3 on Ubuntu 10.04, simply using the desctructor without template embellishments compiles and runs for me:
tab.~deque();
Is there something I'm missing?
I can't comment on other's posts yet, but rodrigo's answer is great. Further to that, here is a rather long but informative SO post on the topic of explicit destructors in templated contexts.
It's usually easier to write a function to deduce a type than try to specify it yourself. How about:
template <typename T>
void murder(T& t)
{ t.~T(); }
In C++11, you can just use the injected class name:
tab.~deque();

Old code : access arguments of a function passed as an argument to another function?

I have to maintain an old piece of code which does not work correctly anymore when compiled on 64k machines.
I have a function "solve" calling another function "funct" passed as a pointer:
int solve(double*x,double xA,double xB,double zeps,double funct(double x,double*),...)
Therefore, "solve" can be used with different possible functions as for example :
double isDgood(double D,double*Y);
From within the function "solve", it was possible to call the function "funct" and access its arguments using:
fA=funct(xA,(double*)(&funct+1));"
Although I am not familiar with this syntax, I guess that the developer was assuming that the unspecified arguments were just pushed in the stack. However, this code does not work anymore on 64k platforms. How can I correct this code? Should I specifically use Va_list?
Thanks you very much for your help.
That's horrifically undefined behaviour. If you want to access the argument, you will have to pass them around.

about argument to member function

Will adding an argument to member function gonna change the behavior of the function?
Need help about this concept.
The behavior won't change if you only add an argument to the prototype of the function.
However you will have to change all the call to this function since the prototype changed, this isn't the same function anymore.
And sincerely, I don't see the point of adding an argument if you don't modify the code afterward.
You'll need to give us more information. Like an example. But say you add a new argument (and provide a default), and now argument will be "constructed" and destructed everywhere the member function was called. So it's possible that that in itself has side-effects (e.g. print to console on creationg and destruction).

Accessing a Pointer to a pointer in C++

Hi I am using a 3rd party library in my iPhone application that uses C++ one of the methods i need to use returns a pointer to a pointer of a class. like follows.
DLL classAttributes** getAttributes();
I can successfully call the method and return the value into a pointer to a pointer like so;
classAttributes **attributes = cPPClass->getAttributes();
however i can't seem to access any of the methods on the class for example i know that the class has a function called getName(); but when i try calling it i get errors.
attributes->getName(); 'Request for member 'getName' in *attributes'; which is of non-class type 'attributes*''
I did some googling and found some stuff that said to access a pointer pointer address use the format
&(*attributes)->getName(); 'invalid uses of incomplete type 'struct attributes'
Any help would be much appreciated.
Thanks.
'invalid uses of incomplete type 'struct attributes' indicates that you need to #include the header file for it. It's only forward-declared at this point in your code.
What you probably want is a classAttributes*. Since you have a classAttribues** , you want to dereference it once to get to the classAttributes*.
(*attributes)
From there you can probably call the members of the class.
classAttributes **attributesArray = cPPClass->getAttributes();
classAttributes *attributesObject(*attributesArray);
attributesObject->getName();
First consider what you would do if the function returned a pointer to a classAttributes variable. In this case, you would simply do:
attributes->getName();
Since it's a pointer to a pointer, you must first dereference it and then use the -> operator:
(*attributes)->getName();
If this doesn't work, it may be a problem caused by the function not being implemented or by inheritance.
Without knowing more about the library, it's hard to say. But it could be returning an array of items:
attributes[0]->getName();
attributes[1]->getName();
The questionable part about my guess, though, is that there is no obvious way to know how many items there are.
By the looks of it, you'd probably have to do is this way (i.e. no ampersand):
(*attributes)->getName();
This is because dereferencing it once will give you a pointer to a struct, on which you can then use the -> operator. What's happening when you do &(*attributes) is that you're dereferencing the pointer, then getting the address of the object you have — in effect making it a no-op.

(Obj) C++: Instantiate (reference to) class from template, access its members?

I'm trying to fix something in some Objective C++ (?!) code. I don't know either of those languages, or any of the relevant APIs or the codebase, so I'm getting stymied left and right.
Say I have:
Vector<char, sizeof 'a'>& sourceData();
sourceData->append('f');
When i try to compile that, I get:
error: request for member 'append' in 'WebCore::sourceData', which is of non-class type 'WTF::Vector<char, 1ul >& ()();
In this case, Vector is WTF::Vector (from WebKit or KDE or something), not STD::Vector. append() very much is supposed to be a member of class generated from this template, as seen in this documentation. It's a Vector. It takes the type the template is templated on.
Now, because I never write programs in Real Man's programming languages, I'm hella confused about the notations for references and pointers and dereferences and where we need them.
I ultimately want a Vector reference, because I want to pass it to another function with the signature:
void foobar(const Vector<char>& in, Vector<char>& out)
I'm guessing the const in the foobar() sig is something I can ignore, meaning 'dont worry, this won't be mangled if you pass it in here'.
I've also tried using .append rather than -> because isn't one of the things of C++ references that you can treat them more like they aren't pointers? Either way, its the same error.
I can't quite follow the error message: it makes it sound like sourceData is of type WTF:Vector<char, 1ul>&, which is what I want. It also looks from the those docs of WTF::Vector that when you make a Vector of something, you get an .append(). But I'm not familiar with templates, either, so I can't really tell i I'm reading that right.
EDIT:
(This is a long followup to Pavel Minaev)
WOW THANKS PROBLEM SOLVED!
I was actually just writing an edit to this post that I semi-figured out your first point after coming across a reference on the web that that line tells the compiler your forward declaring a func called sourceData() that takes no params and returns a Vector of chars. so a "non-class type" in this case means a type that is not an instance of a class. I interpreted that as meaning that the type was not a 'klass', i.e. the type of thing you would expect you could call like .addMethod(functionPointer).
Thanks though! Doing what you suggest makes this work I think. Somehow, I'd gotten it into my head (idk from where) that because the func sig was vector&, I needed to declare those as &'s. Like a stack vs. heap pass issue.
Anyway, that was my REAL problem, because I tried what you'd suggested about but that doesn't initialize the reference. You need to explicitly call the constructor, but then when I put anything in the constructor's args to disambiguate from being a forward decl, it failed with some other error about 'temporary's.
So in a sense, I still don't understand what is going on here fully, but I thank you heartily for fixing my problem. if anyone wants to supply some additional elucidation for the benefit of me and future google people, that would be great.
This:
Vector<char, sizeof 'a'>& sourceData();
has declared a global function which takes no arguments and returns a reference to Vector. The name sourceData is therefore of function type. When you try to access a member of that, it rightfully complains that it's not a class/struct/union, and operator-> is simply inapplicable.
To create an object instead, you should omit the parentheses (they are only required when you have any arguments to pass to the constructor, and must be omitted if there are none):
Vector<char, sizeof 'a'> sourceData;
Then you can call append:
sourceData.append('f');
Note that dot is used rather than -> because you have an object, not a pointer to object.
You do not need to do anything special to pass sourceData to a function that wants a Vector&. Just pass the variable - it will be passed by reference automatically:
foobar(sourceData, targetData);
Dipping your toes in C++ is never much fun. In this case, you've run into a couple of classic mistakes. First, you want to create an instance of Vector on the stack. In this case the empty () is interpreted instead as a declaratiton of a function called sourceData that takes no agruments and returns a reference to a Vector. The compiler is complaining that the resulting function is not a class (it's not). To create an instance of Vector instead, declare the instance without the () and remove the &. The parentheses are only required if you are passing arguments to the instance constructor and must be omitted if there are no arguments.
You want
Vector<char, sizeof 'a'> sourceData;
sourceData.append('f');
Vector<char, sizeof 'a'> outData; //if outData is not instantiated already
foobar(sourceData, outData);
This Wikipedia article gives a decent introduction to C++ references.