Jump to content

TL:DR; I'm not looking to have someone do my homework.

 

I'm not very experienced in C++. I'm doing a lab assignment for class. It "works" well enough, but I know that I'm including improperly. I feel that I should be able to include the header file and be fine; however, whenever I do that, I get the following error:

Quote

/home/nachos/code/build.linux/../threads/threadtest.cc:12: undefined reference to `Stack::Stack(int)'
/home/nachos/code/build.linux/../threads/threadtest.cc:18: undefined reference to `Stack::Push(int)'
/home/nachos/code/build.linux/../threads/threadtest.cc:19: undefined reference to `Stack::GetSize()'
/home/nachos/code/build.linux/../threads/threadtest.cc:31: undefined reference to `Stack::GetSize()'
/home/nachos/code/build.linux/../threads/threadtest.cc:35: undefined reference to `Stack::GetSize()'
/home/nachos/code/build.linux/../threads/threadtest.cc:36: undefined reference to `Stack::GetSize()'
/home/nachos/code/build.linux/../threads/threadtest.cc:37: undefined reference to `Stack::Pop()'
/home/nachos/code/build.linux/../threads/threadtest.cc:39: undefined reference to `Stack::GetSize()'
/home/nachos/code/build.linux/../threads/threadtest.cc:40: undefined reference to `Stack::GetSize()'
collect2: error: ld returned 1 exit status
Makefile:344: recipe for target 'nachos' failed
make: *** [nachos] Error 1

But, if I #include stack.cc, it compiles correctly and runs as expected. Can someone please educate me and tell me what I'm missing? I feel like it has to be something super small. I've modified the Makefile to include everything (these files are added to a pre-existing file). Any guidance would be great. Thanks in advance.

 

//stack.h

#ifndef STACK_H
#define STACK_H


class Stack {
public:
        Stack(int sz);
        // Constructor: initialize variables, allocate space.
        void Push(int value);
        // Push an integer, checking for overflow.
        void Pop();
        // Pop the top value of the stack
        bool Full();
        // Returns TRUE if the stack if full, FALSE otherwise.
        int GetSize();
        // Returns the size of the Stack
private:
        int size;
        // The maximum capacity of the stack.
        int top;
        // Index of the top of the stack.
        int *stack;
        // A pointer to an array that holds the contents.
};

#endif //STACK_H
#include "stack.h"
void Stack::Stack(int sz) {
    size = sz;
    top = 0;
    stack = new int[size];
// an array of integers of size "size"
}
bool Stack::Full() {
    return (top == size);
}
void Stack::Push(int value) {
    ASSERT(!Full());
        // ASSERT(!this->Full())
    stack[top++] = value;
        // this->stack[this->top++] = value
}

void Stack::Pop() {
//    if (top != 0) {
//        top = (top - 1);
//    }
    if (top > 0) {
        stack[top--];
    }
}

int Stack::GetSize() {
    return top;
}

And the test class it's being implemented in.

#include "kernel.h"
#include "main.h"
#include "thread.h"
#include "stack.h"

void
SimpleThread(int which)
{

    printf("Version 1");
    int num;
    Stack *s = new Stack(10);


    for (num = 0; num < 5; num++) {
        printf("*** thread %d looped %d times\n", which, num);
        printf("Hello World!\n");
        s->Push(num);
        printf("Size of the stack: %d \n", s->GetSize());

//        while( !s.empty() ) {
//             cout << s.top() << " ";
//        }

//        s->Pop();
        //printf("Stack size is: %d \n", s.size());

        kernel->currentThread->Yield();
    }

    printf("Size of the stack: %d \n", s->GetSize());

    printf("Emptying Stack:\n");

    while (s->GetSize() > 0) {
        printf("Items Remaining on Stack: %d \n", s->GetSize());
        s->Pop();
    }
    if (s->GetSize() == 0) {
        printf("Items Remaining on Stack: %d \n", s->GetSize());
        delete s;
    }
}

void
ThreadTest()
{
   // Stack *s = new Stack(5);
    Thread *t = new Thread("forked thread");
    t->Fork((VoidFunctionPtr) SimpleThread, (void *) 1);

    SimpleThread(0);
}

 

Link to comment
https://linustechtips.com/topic/969495-c-include-issue/
Share on other sites

Link to post
Share on other sites

One thing I noticed is

void Stack::Stack(int sz){
    size = sz;
    top = 0;
    stack = new int[size];
// an array of integers of size "size"
}

in I assume stack.cpp. That void shouldn't be there as constructors don't have return types.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
https://linustechtips.com/topic/969495-c-include-issue/#findComment-11729943
Share on other sites

Link to post
Share on other sites

In C++ compiling happens in compilation units.

Header files are used to publish types and function signatures that a compilation unit wants to have published and source files actually implement them. This is done so that a change in one compilation unit's implementation won't require one to recompile all the other ones as well (which might take even hours if a project is huge).

 

Taking real advantage of this requires using Make or some other build tool, so it's really not that important for smaller projects.

 

However, even in smaller projects doing includes the de facto way is clean, so:

1. Only include header files. This helps to keep compilation units clean (no fear of double definitions, etc.)

2. When compiling a smallish project without the help of a build system, compiling all compilation units every time is fine: 

gcc -o a.out stack.cc some_other_file.cc threadtest.cc

 

I assume the trouble (on the side of what 2FA pointed out) is that one of the source files is missing or there's some other problem with the Makefile. Like not compiling a compilation unit to an object file. So try to do it manually and see if that works.

Edited by Nutler
Noticed that Makefile is being used.....
Link to comment
https://linustechtips.com/topic/969495-c-include-issue/#findComment-11730331
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×