Thread: Chapter 21 Help

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    3

    Chapter 21 Help

    Hi,

    I am getting a build error when I break the code in the book down to a .h file and 2 .cpp files and I'm not sure why. I am following the steps in the book fairly accurately I think. I run the program through orig.cpp and I get the following build error.

    mingw32-g++.exe -c X:\Users\Notneeded\jumpcplusplus\orig.cpp -o X:\Users\Notneeded\jumpcplusplus\orig.o

    mingw32-g++.exe -o
    X:\Users\Notneeded\jumpcplusplus\orig.exe X:\Users\DSoutherland\jumpcplusplus\orig.o

    X:\Users\Notneeded\jumpcplusplus\orig.orig.cpp.text+0x91): undefined reference to `addNode(Node*, int)'

    X:\Users\Notneeded\jumpcplusplus\orig.orig.cpp.text+0xb4): undefined reference to `printList(Node const*)'

    collect2.exe: error: ld returned 1 exit status


    I thought by #include "linkedlist.h" in the orig.ccp code it would allow communication between the linkedlist.h and the linkedlist.cpp but it doesn't appear that the orig.cpp file has access to the logic of the functions.

    Wondering what I am missing?

    (s), 0 warning(s) (0 minute(s), 1 second(s))
    linkedlist.h
    Code:
    
    
    Code:
    struct Node
    {
        Node *p_next;
        int value;
    };Node *addNode (Node* p_list, int value);
    void printList (const Node* p_list);


    linkedlist.cpp
    Code:
    #include <iostream>
    #include "linkedlist.h"
    using namespace std;
    Node *addNode (Node* p_list, int value)
    {
        Node *p_new_node = new Node;
        p_new_node->value = value;
        p_new_node->p_next = p_list;
        return p_new_node;
    }
    void printList (const Node* p_list)
    {
        const Node* p_cur_node = p_list;
        while (p_cur_node != NULL)
        {
            cout << p_cur_node->value << endl;
            p_cur_node = p_cur_node->p_next;
        }
    }



    orig.cpp
    Code:
    
    
    Code:
    #include <iostream>
    #include "linkedlist.h"
    using namespace std;
    int main ()
    {
        Node *p_list = NULL;
        for ( int i = 0; i<10; ++i)
        {
            int value;
            cout << "Enter value for list node: ";
            cin >> value;
            p_list = addNode(p_list, value);
        }
        printList(p_list);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    It appears that you need to add linkedlist.cpp to your build line.

    Jim

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Hi Jim,

    In orig.cpp I've replaced #include "linkedlist.h" with #include "linkedlist.cpp" and the program works. I don't know if you are familiar with the book but it says never include a .cpp file directly due to the compiler seeing multiple definitions of the same function. I'm having trouble visualizing that statement and whether or not I am taking a bad shortcut?

    Thanks,

    Dave

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should not include the .cpp file. You should compile it.
    g++ orig.cpp linkedlist.cpp -o myexe.exe
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,974
    Quote Originally Posted by djsuthe View Post
    Hi Jim,

    In orig.cpp I've replaced #include "linkedlist.h" with #include "linkedlist.cpp" and the program works. I don't know if you are familiar with the book but it says never include a .cpp file directly due to the compiler seeing multiple definitions of the same function. I'm having trouble visualizing that statement and whether or not I am taking a bad shortcut?

    Thanks,

    Dave
    You are right, this is the wrong approach.

    Your three command lines should have been
    Code:
    mingw32-g++.exe -c X:\Users\Notneeded\jumpcplusplus\orig.cpp -o X:\Users\Notneeded\jumpcplusplus\orig.o
    mingw32-g++.exe -c X:\Users\Notneeded\jumpcplusplus\linkedlist.cpp -o X:\Users\Notneeded\jumpcplusplus\linkedlist.o
    mingw32-g++.exe -o X:\Users\Notneeded\jumpcplusplus\orig.exe X:\Users\DSoutherland\jumpcplusplus\orig.o X:\Users\Notneeded\jumpcplusplus\linkedlist.o
    Later on, you will learn about makefiles, or project files, which will automate this process for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++// Programming Challenge #11 in Chapter 8
    By Selena Yapura in forum C++ Programming
    Replies: 7
    Last Post: 04-23-2014, 02:43 PM
  2. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM
  3. More Chapter 5 practice problems!
    By tsdad in forum C++ Programming
    Replies: 13
    Last Post: 05-17-2013, 11:06 AM
  4. K&R chapter 8
    By Tool in forum C Programming
    Replies: 9
    Last Post: 02-22-2010, 02:05 PM
  5. Visual C++ 6 Bible [Chapter 7]
    By Dual-Catfish in forum Windows Programming
    Replies: 6
    Last Post: 03-12-2002, 01:15 PM

Tags for this Thread