csci>g++ -c queue.cpp
csci>g++ queue.o project6.cpp
Undefined first referenced
symbol in file
Queue<int>::return_index() /var/tmp//ccHbZwrM.o
Queue<int>::enqueue(int) /var/tmp//ccHbZwrM.o
Queue<int>::dequeue() /var/tmp//ccHbZwrM.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status


These are my errors. As you can see, my implementation file compiles, but does not compile with the client program. What is going on? I checked all the templates and they seem fine. Can anyone point me the right direction.

queue.h

#ifndef QUEUE_H
#define QUEUE_H

const int MAX = 100;


template <class Item>
class Queue{


public:

        Queue()
        {
                front = 0;
                rear = MAX - 1;
                count = 0;
                data[MAX] = 0;
        }
       void display();
        void enqueue(Item);
        Item dequeue();
        int return_index();



private:
        Item data[MAX];
        int front, count, rear;
        int next_index(int i)
        {
                return (i + 1) % MAX;
        }


};
#endif

queue.cpp(implementation file)

#include "queue.h"
#include <iostream>
using namespace std;

template <class Item>
void Queue<Item>::display()
{
        int index = front;

        for (int i = 0; i < count; ++i)
        {
                cout << data[index] << endl;
                index = next_index(index);
        }

}

template <class Item>
void Queue<Item>::enqueue(Item entry)
{
        rear = next_index(rear);
        data[rear] = entry;
        ++count;
}

template <class Item>
Item Queue<Item>::dequeue()
{
        Item dequeued;
        dequeued = data[front];
        front = next_index(front);
        --count;
        return dequeued;
}

template <class Item>
int Queue<Item>::return_index()
{
        return count;
}

project6.cpp(client file)

#include <iostream>
#include "queue.h"
#include <iomanip>
using namespace std;

int get_lowest(Queue<int> [], int);

int main()
{
        int qs_pair, maxtran, prob, dur, seed;
        int count(0);                    // The number of customers served
        int entry_time(0);               // When each served customer arrived
        int wait_sum(0);                 // Sum of waiting times


        cout << "how many queue/server pair(s)? ";
        cin >> qs_pair;
        cout << "\nthe max time for customer transaction --> ";
        cin >> maxtran;
        cout << "\nthe probability a customer will arrive --> ";
        cin >> prob;
        cout << "\nduration of simulation(at least 1) --> ";
        cin >> dur;
        cout << "\nenter an integer seed --> ";
        cin >> seed;

        srand(seed);
        Queue<int> line[qs_pair]; //needs fixing
        int trans_time[qs_pair]; //time remaining in a transaction

        for(int i = 0; i < qs_pair; i++)// needs fixing
        {
                trans_time[i] = 0;
        }

        int lowest;
        int trans_time_c; // trans_time counter

        for(int time = 0; time < dur; time++)
        {
                cout << "Tick " << time << " " << endl;

                if(rand() % 100 < prob)
                {

                        lowest = get_lowest(line, qs_pair);

                        line[lowest].enqueue(time);
                }

                //good above

                for(trans_time_c = 0; trans_time_c < qs_pair; trans_time_c++)
                {
                        if(trans_time[trans_time_c] == 0)
                        {
                                if(line[trans_time_c].return_index() != 0)
                                {
                                        entry_time = line[trans_time_c].dequeue();
                                        wait_sum += (time - entry_time);
                                        ++count;
                                        trans_time[trans_time_c] = (rand() % maxtran) + 1;
                                }
                        }
                        else
                        {
                                trans_time[trans_time_c] -= 1;
                        }


                }
                for(int i = 0; i < qs_pair; i++)//needs fixing
                {
                        cout << "line " << i << " trans time " << trans_time[i] << endl;
                }
                cout << endl;

        }


}

int get_lowest(Queue<int> line[], int qs_pair)
{
        int temp = line[0].return_index();
        int temp_i = 0;

        for(int i = 0; i < qs_pair; i++)
        {
                if(line[i].return_index() == 0)
                        return i;
                else if((i == qs_pair - 1) && line[qs_pair - 1].return_index() < temp)
                {
                        temp = line[qs_pair - 1].return_index();
                        temp_i = qs_pair - 1;
                }
                else
                {

                        if(line[i].return_index() > temp)
                        {

                        }
                        else
                        {
                                temp = line[i].return_index();
                                temp_i = i;
                        }
                }
        }
        return temp_i;
}

Recommended Answers

All 3 Replies

anyone?

you should right away notice that it seems to be complaining about templates .... hit up either C++ Primer byt Lippman/Lajoie or Bjarne's book

keywords to search for on the web "inclusion compilation model"

to summarize "to generate an instantiation, the compiler must have acess to the source code that defines the template"

sooo .... you can try adjusting either your class definition OR your includes so that the compiler "sees" the templated code it needs to see when running through your project6 file code

commented: thanks , I dig some of your posts and get something to read. +1

you should right away notice that it seems to be complaining about templates .... hit up either C++ Primer byt Lippman/Lajoie or Bjarne's book

keywords to search for on the web "inclusion compilation model"

to summarize "to generate an instantiation, the compiler must have acess to the source code that defines the template"

sooo .... you can try adjusting either your class definition OR your includes so that the compiler "sees" the templated code it needs to see when running through your project6 file code

thank you very much! now i remember what the old man was talking about in class! I only have to compile the client program!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.