C2086 error: static local variables in member functions 
Author Message
 C2086 error: static local variables in member functions

I am having some problems with a function that declares several static local
variables in a member function of a class.  If the variables are of non-POD
class type, then every 8th such variable results in the following error
message:

error C2086: '.local static guard.' : redefinition

For instance, consider the following code:

// StaticTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
using namespace std;

struct stupid
{
 stupid() {
  static string a;
  static string b;
  static string c;
  static string d;
  static string e;
  static string f;
  static string g;
  static string h;
  static string i;
  static string j;
  static string k;
  static string l;
  static string m;
  static string n;
  static string o;
  static string p;
  static string q;
  static string r;
  static string s;
  static string t;
  static string u;
 }

Quote:
};

This results in a 2086 error for the lines where h and p are defined
(although the error message does not mention the variable names).  Through
experimentation, I have found that the function the static variables are
declared in must be a non-static class member function, and the static
variables must be of non-POD class type for the error to occur.  Has anyone
encountered this problem before?

Joe Gottman



Tue, 19 Aug 2003 08:53:12 GMT  
 C2086 error: static local variables in member functions

Quote:

>I am having some problems with a function that declares several static local
>variables in a member function of a class.  If the variables are of non-POD
>class type, then every 8th such variable results in the following error
>message:

>error C2086: '.local static guard.' : redefinition

>For instance, consider the following code:

>// StaticTest.cpp : Defines the entry point for the console application.
>//

>#include "stdafx.h"
>#include <string>
>using namespace std;

>struct stupid
>{
> stupid() {
>  static string a;
>  static string b;
>  static string c;
>  static string d;
>  static string e;
>  static string f;
>  static string g;
>  static string h;
>  static string i;
>  static string j;
>  static string k;
>  static string l;
>  static string m;
>  static string n;
>  static string o;
>  static string p;
>  static string q;
>  static string r;
>  static string s;
>  static string t;
>  static string u;
> }
>};

>This results in a 2086 error for the lines where h and p are defined
>(although the error message does not mention the variable names).  Through
>experimentation, I have found that the function the static variables are
>declared in must be a non-static class member function, and the static
>variables must be of non-POD class type for the error to occur.  Has anyone
>encountered this problem before?

Sounds like a bug, but IMO it's better practice to define such
functions out of line when possible. When I define stupid() outside
the class body, the problem doesn't occur, unless I also declare
stupid() as inline.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Tue, 19 Aug 2003 13:47:30 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. static function access member variable and member function

2. Watch a local static variable outside of function?

3. static local variables vs global variables

4. static variable defined in a member function

5. Getting a Linking error when using a static member variable in a DLL

6. a static member variable error

7. Illegal call of non-static member function (error)?

8. Error C2352 - illegal call of non-static member function

9. PostMessage compile error: non-static member function

10. Compiler Error C2352: illegal call of non-static member function

11. member function and member variable

12. member function and member variable

 

 
Powered by phpBB® Forum Software