0

I am currently attempting to implement a Doubly Linked List for a Data Structures class.

I currently have implemented a generic Node* class and wanted to hold an instance of another class Student(int i, int j) that I have implemented.

This is what i'm attempting to do in my main method:

Student student1 = Student(10,11);
Node<Student()> node1 = Node<Student()> (student1);

This is the error I am receiving. Everything worked absolutely fine when the Node was holding a primitive data type like an int but i'm unsure how to account for the difference between storing an int and storing a Student object.

I would love any insight or pushes in the right direction. Thank you.

enter image description here

Here is my implementation of Node.

#ifndef NODE_H
#define NODE_H

template <class T>
class Node
{
    public:
    Node();
    Node(T k);
    virtual~Node();

    T key;
    Node<T>* prev;
    Node<T>* next;
};
#endif

//default constructor
template <class T>
Node<T>::Node()
{
    prev = NULL;
    next = NULL;
}

 template <class T>
Node<T>::Node(T k)
{
    prev = NULL;
    next = NULL;
    key = k;
}

template<class T>
Node<T>::~Node()
{
//implement 
}

Student.cpp

#include "Student.h"

Student::Student()
{
    mArrivalTime = 0;
    mTimeNeeded = 0;
}
Student::Student(int arrivalTime, int timeNeeded)
{
    mArrivalTime = arrivalTime;
    mTimeNeeded = timeNeeded;
}

Student::~Student()
{
     //implement
}

int Student::getArrivalTime()
{
    return mArrivalTime;
}

int Student::getTimeNeeded()
{
    return mTimeNeeded;
}

Student.h

#ifndef STUDENT_H
#define STUDENT_H

using namespace std;
class Student
{
    private:
        int mArrivalTime;
        int mTimeNeeded;

     public:
        Student();
        Student(int arrivalTime, int timeNeeded);
        ~Student();
        int getArrivalTime();
        int getTimeNeeded();
};
#endif
Daniel Cole
  • 49
  • 1
  • 8

1 Answers1

3

Don't use (), just use the class name. e.g:

Node<Student> node1 = Node<Student> (student1);
The Dark
  • 8,453
  • 1
  • 16
  • 19
  • I was curious as to if this would work, but when I do I receive this error: Undefined symbols for architecture x86_64: "Student::Student(int, int)", referenced from: _main in Assignment4-1de395.o "Student::Student()", referenced from: Node::Node(Student) in Assignment4-1de395.o "Student::~Student()", referenced from: _main in Assignment4-1de395.o Node::~Node() in Assignment4-1de395.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – Daniel Cole Apr 11 '16 at 02:40
  • That is a linker error, which probably means that the compile step worked. I would guess that you need to add Student.cpp to your `g++` command line, so it is linked in with your main .cpp file. – The Dark Apr 11 '16 at 02:46
  • See http://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files for more info – The Dark Apr 11 '16 at 02:46
  • Ahhh, of course. I got so used to working with templates for this assignment and not having to compile header files that I forgot entirely about Student.cpp needing to be compiled. Thank you. – Daniel Cole Apr 11 '16 at 02:53