Thread & Classes

This is a discussion on Thread & Classes within the C++ Programming forums, part of the General Programming Boards category; You have cycles in your headers. All header files rely on Share.hpp, but Share.hpp includes the others that depends on ...

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    You have cycles in your headers. All header files rely on Share.hpp, but Share.hpp includes the others that depends on it.
    You need to break those cycles.

    >>NetworkHandler NHandler; //<----------- ERROR : NetworkHandler does not name a type
    This should not be in a header. Global variables should be in .cpp files. Then you put
    extern NetworkHandler NHandler;
    in your header.

    You might be able to get away with doing

    class NetworkHandler;

    before your extern declaration.
    Or you simply break it off into another header file.
    And I don't see why Share.hpp needs ClientHandler.hpp.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    You have cycles in your headers. All header files rely on Share.hpp, but Share.hpp includes the others that depends on it.
    You need to break those cycles.

    >>NetworkHandler NHandler; //<----------- ERROR : NetworkHandler does not name a type
    This should not be in a header. Global variables should be in .cpp files. Then you put
    extern NetworkHandler NHandler;
    in your header.

    You might be able to get away with doing

    class NetworkHandler;

    before your extern declaration.
    Or you simply break it off into another header file.
    And I don't see why Share.hpp needs ClientHandler.hpp.
    Looks like #ifndef don't work in other headers?

    Well so i put
    Code:
    class NetworkHandler;
    extern NetworkHandler NHandler;
    in my header, then i need to initilialize it somehow in share.cpp ?

    When i try use it in main.cpp
    Code:
    NHandler.Init();
    it says undefinied reference

  3. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by kargo View Post
    Looks like #ifndef don't work in other headers?
    Yes, they do, but they won't work as you expect in this case. Consider the code:

    A.h:
    Code:
    #ifndef TEMP_20110316_1259
    #define TEMP_20110316_1259
    
    #pragma message( "Pre-include: Including A.h..." )
    #include "Temp2.h"
    #pragma message( "Post-include: Including A.h..." )
    
    class A 
    {
    	B b;
    };
    
    #endif
    B.h:
    Code:
    #ifndef TEMP2_20110316_1259
    #define TEMP2_20110316_1259
    
    #pragma message( "Pre-include: Including B.h..." )
    #include "Temp.h"
    #pragma message( "Post-include: Including B.h..." )
    
    class B
    {
    	A a;
    }
    
    #endif
    When compiling, I get this output:
    2> Pre-include: Including A.h...
    2> Pre-include: Including B.h...
    2> Post-include: Including B.h...
    error C2146: syntax error : missing ';' before identifier 'a'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    2> Post-include: Including A.h...
    error C2236: unexpected 'class' 'A'. Did you forget a ';'?
    error C2143: syntax error : missing ';' before '{'
    error C2447: '{' : missing function header (old-style formal list?)

    What is happening?
    A.h includes B.h.
    B.h includes A.h.
    B.h tries to include A.h. But the define has been defined, so it skips including its contents.
    Here is the problem. Because A.h included B.h before the actual contents of the file, the declaration of A is missing at this point.
    B.h assumes it has the declaration of A at this point and tries to use it, but it doesn't exist, so we get an error.

    Well so i put
    Code:
    class NetworkHandler;
    extern NetworkHandler NHandler;
    in my header, then i need to initilialize it somehow in share.cpp ?
    Yes, you must put
    NetworkHandler NHandler;
    inside a .cpp file. Any source file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    So how can i do this?

    Share.h
    Code:
    #include ...
    #include ...
    
    //Check if include caller is NetworkHandler
    //if it is then dont do nothing here
    //if it is not then :
    #include "networkhandler.hpp"
    //done

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Why do it? Split your header files.
    Put all common code into a specific header.
    Then make two special headers: one for NetworkHandler, and one for the rest.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    Why do it? Split your header files.
    Put all common code into a specific header.
    Then make two special headers: one for NetworkHandler, and one for the rest.
    I did like this :

    - deleted from share.hpp
    ... #include "networkhandler.hpp"
    ... #include "clienthandler.hpp"
    - I'm including share.hpp in cpp files for NHandler and CHandler
    - In ClientHandler.cpp I do include NetworkHandler.h
    - In NetworkHandler.cpp I do include ClientHandler.h

    Is it good idea?

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    There are cycles in NetworkHandler.h and ClientHandler.h, is there? If not, and if those files need them, then it's good.
    (Although, it is good programming practice to cut ties between different classes as much as possible.)
    I don't know what NHandler and CHandler is, or what share.hpp is supposed to contain, so I can't say anything about that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    NHandler is NetworkHandler class which is responsible for make things like Init socket, set listener etc.
    CHandler is ClientHandler class which is responsible for serving each client.
    In share.hpp i have some includes like winsock etc. and declared some types and struct like Packet and Client.

  9. #39
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    What's the fastest way to move data from structure?

    Like i call function which has pointer to my struct as argument, but into this function i want to move all structure to another variable (non pointer).

    Like :

    Code:
    void func(PPACKET pck)
    {
        TPACKET packet;
        packet = &pck;
    }

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Your code makes no sense. Anyway...
    To make a copy, you simply do an assignment: a = b
    If you don't want a copy, but merely another variable pointing to the same data structure, you can use a reference, say: MyType& a = b.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    Your code makes no sense. Anyway...
    To make a copy, you simply do an assignment: a = b
    If you don't want a copy, but merely another variable pointing to the same data structure, you can use a reference, say: MyType& a = b.
    Yeah lol it should be * instead of &, my bad

    Well i could but its like i use some struct for receive data from client and i need to free it as fast as possible to not block another packets. And again to this same variable i assign new data and again, again, again.

    So I think i would just copy data from pointer, start thread and return from function. So i have free variable again to use for next incoming data and i'm processing last one in thread.

    Anyway i'll have to do some safe about using the same var read/write to it from threads.

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Boost has synchronizing functionality, as well. Just a heads up.
    But from your description, it seems that you should just make a copy of the data received, or alternatively, tell your sockets to write the data directly to where you want to store it in order to process it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Boost has synchronizing functionality, as well. Just a heads up.
    But from your description, it seems that you should just make a copy of the data received, or alternatively, tell your sockets to write the data directly to where you want to store it in order to process it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-09-2010, 08:31 PM
  2. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 02:44 AM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 01:16 PM
  4. Replies: 12
    Last Post: 05-17-2003, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21