Undefined Reference to

后端 未结 6 920
生来不讨喜
生来不讨喜 2020-12-01 06:28

When I compile my code for a linked list, I get a bunch of undefined reference errors. The code is below. I have been compiling with both of these statements:



        
6条回答
  •  旧时难觅i
    2020-12-01 07:06

    1. Usually headers guards are for header files (i.e., .h ) not for source files ( i.e., .cpp ).
    2. Include the necessary standard headers and namespaces in source files.

    LinearNode.h:

    #ifndef LINEARNODE_H
    #define LINEARNODE_H
    
    class LinearNode
    {
        // .....
    };
    
    #endif
    

    LinearNode.cpp:

    #include "LinearNode.h"
    #include 
    using namespace std;
    // And now the definitions
    

    LinkedList.h:

    #ifndef LINKEDLIST_H
    #define LINKEDLIST_H
    
    class LinearNode; // Forward Declaration
    class LinkedList
    {
        // ...
    };
    
    #endif
    

    LinkedList.cpp

    #include "LinearNode.h"
    #include "LinkedList.h"
    #include 
    using namespace std;
    
    // Definitions
    

    test.cpp is source file is fine. Note that header files are never compiled. Assuming all the files are in a single folder -

    g++ LinearNode.cpp LinkedList.cpp test.cpp -o exe.out
    

提交回复
热议问题