1

I have problem with this code:

// Make it Non Copyable 


FileLogger (const FileLogger &) = delete;
FileLogger &operator= (const FileLogger &) = delete;

I need solve to this error:

Error   1   error C2059: syntax error : ';' 
Error   2   error C2238: unexpected token(s) preceding ';'  

This error happen 16 time with same syntax in the same code line posted above.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • 3
    Which version of visual C++ compiler are you using? – greatwolf Oct 24 '13 at 23:38
  • 1
    What are you expecting to do exactly? This synthax seems wrong in so many ways I don't even... – Havenard Oct 24 '13 at 23:39
  • 1
    I don't understand that syntax at all. What are you trying to do? Is FileLogger a function, and if so how can you assign it to "delete"? Post more info so maybe I can help. – Rami Oct 24 '13 at 23:39
  • 3
    @Havenard That should be valid syntax in [C++11](http://stackoverflow.com/questions/9458741/with-explicitly-deleted-member-functions-in-c11-is-it-still-worthwhile-to-inh). – greatwolf Oct 24 '13 at 23:40
  • @greatwolf Really? And what exactly are those supposed to mean? – Havenard Oct 24 '13 at 23:43
  • @Havenard: Deleting a function means that you're defining it not to exist, so that any code that tries to call it gives a compile-time error. In this case, it's making is impossible to copy the class, as the comment says. But it won't work with a pre-2011 compiler, since it's a fairly new language feature. – Mike Seymour Oct 24 '13 at 23:44
  • @Havenard Also check out the question I linked in my second comment. – greatwolf Oct 24 '13 at 23:45
  • @RamiHelmy FileLogger is a class that perform logging text to file – Security Crazy Oct 24 '13 at 23:46
  • @SecurityCrazy ok got it. I think you already have the answer by now :) – Rami Oct 24 '13 at 23:48
  • @MikeSeymour So basically its suppressing the compiler's attempt to create some of the special member functions (copy constructors)? Thats very interesting. How would that be done in a previous version of C++? – Havenard Oct 24 '13 at 23:51
  • Nevermind, poolie answered that too. – Havenard Oct 24 '13 at 23:54

2 Answers2

8

I assume you're trying to use the new C++11 = delete syntax to suppress the default implementation. I think this error means that this compiler just doesn't understand that syntax.

In pre-C++11 compilers the typical approach is to instead use DISALLOW_COPY_AND_ASSIGN macros that end up declaring the constructor/assignment as private.

Community
  • 1
  • 1
poolie
  • 9,289
  • 1
  • 47
  • 74
  • plz , can you tell me more how can i use DISALLOW_COPY_AND_ASSIGN macros in my class . – Security Crazy Oct 24 '13 at 23:58
  • 1
    If you just delete those lines, the code should compile just fine. This code is telling the compiler it has to disallow attempts to copy the class, throwing a compile time error. If you remove the code it will allow copy, but depending on the case that shouldn't be a problem. – Havenard Oct 25 '13 at 00:32
  • @SecurityCrazy [How to disable the copy constructor](http://stackoverflow.com/questions/6077143/disable-copy-constructor) and do the same for `operator=`. – poolie Oct 25 '13 at 03:58
0

In pre-C++11, delete is a reserved word. That means it has special meaning to the compiler and can't be used for anything else. If that's the name of one of your variables, rename it and you should be fine.

Christian Ternus
  • 8,406
  • 24
  • 39