Thread: stucture file defined

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    stucture file defined

    Hi I'm a newbie and I search the forum for a post on using a structure. and I am sorry if I missed a post about this.I have read up on structures and I did not see anything like this code. I kind of understand them and I try to understand other codes to help me to understand and to become a better programmer. I came across this code and I loaded it up in C++ to see how it works, but I get errors. I try to figure it out but like I said I am new at this and was hoping someone out there could explain this or a better way to write it.
    In the header file it has the following code.
    Code:
    typedef struct {
        char* cmd;
        void(*funcptr)(char *arg);
    } cmdStruct_t;    
    
    void link_cmd(void);
    void parse(char *cmd);
    void cmd_reset(char *arg);
    void cmd_help(char *arg);
    void cmd_info(char *arg);

    Then on the main code block it has the following code.


    Code:
    cmdStruct_t CMD_LIST[] = {
        {"reset",cmd_reset},
        {"help",cmd_help},
        {"info",cmd_info},
    };
    When I compile the program I get the following errors.
    error LNK2001: unresolved external symbol "void __cdecl cmd_info(char *)" (?cmd_info@@YAXPAD@Z)
    1>new2.obj : error LNK2001: unresolved external symbol "void __cdecl cmd_help(char *)" (?cmd_help@@YAXPAD@Z)
    1>new2.obj : error LNK2001: unresolved external symbol "void __cdecl cmd_reset(char *)" (?cmd_reset@@YAXPAD@Z).
    it looks to me like they are predefining the structure with a variable. I am trying to figure it out on how it works. could someone please explain. I thought this would be a better way instead of using arrays predefined. Not sure which would be better.
    Thanks
    Dorato
    Last edited by dorato; 02-13-2013 at 10:32 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know what you mean by "predefining the structure," but if you mean initializing, then you are correct.
    The reason this code will not compile is because the cmd_help and cmd_reset are not defined (ie, you have only said they exist, but no implementation exists). Because of this, the linker does not know the address of these functions and therefore cannot put the address of them into the CMD_LIST structure instance.
    Btw, this is very poor style C-code. If you are going to use strings, at least use const char*, or std::string.
    You can also drop the typedef, so:

    Code:
    struct cmdStruct_t
    {
        std::string cmd;
        void(*funcptr)(char *arg);
    };
    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.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Quote Originally Posted by Elysia View Post
    I don't know what you mean by "predefining the structure," but if you mean initializing, then you are correct.
    The reason this code will not compile is because the cmd_help and cmd_reset are not defined (ie, you have only said they exist, but no implementation exists). Because of this, the linker does not know the address of these functions and therefore cannot put the address of them into the CMD_LIST structure instance.
    Btw, this is very poor style C-code. If you are going to use strings, at least use const char*, or std::string.
    You can also drop the typedef, so:

    Code:
    struct cmdStruct_t
    {
        std::string cmd;
        void(*funcptr)(char *arg);
    };
    Hi and thanks yes and I meant initializing. I thought the code was a bit odd and I never seen one written this way. I am new in writing C and I am trying to always learn a better way to program.
    Thanks again
    Dorato

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to stucture?
    By traxy in forum C Programming
    Replies: 5
    Last Post: 09-16-2008, 07:40 PM
  2. Sorting within a stucture
    By Zeff in forum C Programming
    Replies: 11
    Last Post: 11-29-2006, 12:00 AM
  3. user defined file I/O
    By montablac in forum C++ Programming
    Replies: 3
    Last Post: 06-05-2005, 04:26 AM
  4. It isn't possible to assign a vector to a stucture is it?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2002, 02:23 PM