Thread: static class problem.

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Exclamation static class problem.

    I am trying to tie these two classes into my Network classes. Presumably, you should only call WSAStartup/WSACleanup in non-overlapping pairs, so I devised this as a way of transparently handling that problem. The compiler errors are simply undefined references to variables of the "static" class. How can I fix that?


    Code:
    //...this class should only be created by "new", and destroyed with "delete"...NO LOCALS...
    
    class WSAInitializer {
    
      private:
    
     WSAData wsaData;
     bool awaiting_shutdown;
    
      public:
    
    WSAInitializer() {
     awaiting_shutdown = false;
      if(WSAStartup(MAKEWORD(1, 1), &wsaData) == 0) {
       awaiting_shutdown = true;
       }
     }
    
    ~WSAInitializer() {
      if(awaiting_shutdown) {
        WSACleanup();
       }
     }
    };
    
    
    //....
    
    
    //...this should be the only class using a WSAInitializer:: object...
    //...also, balanced Startup()/Cleanup() calls will ensure ref_count's integrity...(use in constructor/destructor pairs).
    class WSA {
    
     private:
     
    static WSAInitializer * instance;
    static int ref_count;
     
     public:
    
    WSA() {
     Announce("Initializing...");
     WSA::ref_count = 0;
     }
    static void Startup() {
      ++WSA::ref_count;
      if(WSA::ref_count == 1) {
        WSA::instance = new WSAInitializer;
        Announce("WSAStartup Initiated...");
       }
     }
    static void Cleanup() {
      --WSA::ref_count;
      if(WSA::ref_count == 0) {
        delete WSA::instance;
        Announce("WSACleanup Initiated...");
       }
     }
    }wsa;  //...added this to quiet the compiler, but no avail.
    
    
    
    
    
    //...EXAMPLE:
    
    
    class Foo {
     public:
     
     Foo() {
      WSA::Startup();
     }
    
     ~Foo() {
      WSA::Cleanup();
     }
    };
    
    
    
    
    
    int main() {
    
     if(condition) {
        
       Foo bar;   //...new WSAInitializer:: born
    
    
          if(condition) {
            
            Foo blat;
    
           }  //...blat's destructor called, WSA::ref_count decrements....
      }  //...bar's destructor called, WSA::destroys current WSAInitializer...
    
    
    return 0;
    }
    Last edited by Sebastiani; 10-16-2002 at 02:24 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    If you have a class with a static member:
    Code:
    class foo
    {
    public:
       static int bar;
    };
    you have to define/construct the static member outside the class:
    Code:
    //in some .cpp file
    int foo::bar = 0;
    Then it will work

    Edit
    What is WSA?
    I could only find one match in my dictionary, but I don't think it means "Wirtschafts- und Sozialausschluss"
    /Edit
    Last edited by Sang-drax; 10-16-2002 at 03:22 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Indeed. Thanks.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I assume it stands for Windows Socket API, and it precedes many of the function names in Winsock.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM