0

I am having a little trouble in C++ with a struct that shall contain a unique_ptr. A part of the struct is

 struct test { std::vector <std::unique_ptr<function>> allFunctions; } 

where

 function 

is another struct. I always get C2280 attempting to reference a deleted function

As you might suspect I am quite new to C++ and have not much experience. And i HAVE TO use an unique_ptr, since this is part of an exercise.

Thank you already for your help! :)

4
  • 2
    On which line do you get that error? I guess its not in the three you show. Commented May 12, 2018 at 9:00
  • it says the error occurs in the standard library memory in line 920 Commented May 12, 2018 at 9:12
  • You may want have a look at that post. Commented May 12, 2018 at 9:16
  • Thank you, I looked at it earlier but I don't really understand what that means for me or what I should do Commented May 12, 2018 at 9:23

1 Answer 1

2

The error is in the code that uses test, i.e. the code that you didn't show.

std::unique_ptr is by definition not copyable (it's unique!). That makes your whole structure not copyable.

If you attempt to copy test somewhere, the compiler will tell you that there is no std::unique_ptr::operator =, which is needed for copying.

For example:

test x; test y = x; // Copying. Error C2280 

Or

void someFunction(test x) {} int main() { test x; someFunction(x); // Copying. Error C2280 } 

Error C2280 'std::unique_ptr<function,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function

The solution is to avoid copying test. You can achieve that by passing it by reference.

void someFunction(test& x) {} int main() { test x; someFunction(x); // OK } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sooo much! I think now I got it! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.