PRB: C2279: Braces Not Valid in Function Default Argument ListLast reviewed: August 8, 1997Article ID: Q93401  | 
	
	
 
 
The information in this article applies to:
 
 SYMPTOMSWhen Microsoft C/C++ compiles an application that specifies a structure in the default arguments for a function, the compilation fails with the following error messages: 
    error C2059: syntax error : '{'
   error C2279: cannot use braces to initialize default arguments
CAUSEAccording to the C++ grammar, the default value for an argument must be an expression. An initializer list, such as that used to initialize a structure, is not an expression. 
 RESOLUTIONDefine a constructor to perform the required initialization. 
 MORE INFORMATIONThe first code example demonstrates the errors listed above. The second code example demonstrates the correct method to initialize the structure. 
 Code Sample 1
    /*
    * Compile options needed: /c
    */
   struct ag_type
   {
      int a;
      float b;
   };
   void func(ag_type arg = {5, 7.0});
Code Sample 2
    /*
    * Compile options needed: /c
    */
   struct ag_type
   {
      int a;
      float b;
      ag_type(int aa, float bb) : a(aa), b(bb) {}
   };
   void func(ag_type arg = ag_type(5, 7.0));
	
	 | 
	
	Additional query words: 8.00 8.00c 9.00 9.10 
 © 1998 Microsoft Corporation. All rights reserved. Terms of Use.  |