943,605 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 363
  • C++ RSS
Mar 22nd, 2009
0

where is my error coming from?

Expand Post »
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
c++ Syntax (Toggle Plain Text)
  1. #ifndef QUEUE_H
  2. #define QUEUE_H
  3.  
  4. const int MAX = 100;
  5.  
  6.  
  7. template <class Item>
  8. class Queue{
  9.  
  10.  
  11. public:
  12.  
  13. Queue()
  14. {
  15. front = 0;
  16. rear = MAX - 1;
  17. count = 0;
  18. data[MAX] = 0;
  19. }
  20. void display();
  21. void enqueue(Item);
  22. Item dequeue();
  23. int return_index();
  24.  
  25.  
  26.  
  27. private:
  28. Item data[MAX];
  29. int front, count, rear;
  30. int next_index(int i)
  31. {
  32. return (i + 1) % MAX;
  33. }
  34.  
  35.  
  36. };
  37. #endif

queue.cpp(implementation file)
c++ Syntax (Toggle Plain Text)
  1. #include "queue.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template <class Item>
  6. void Queue<Item>::display()
  7. {
  8. int index = front;
  9.  
  10. for (int i = 0; i < count; ++i)
  11. {
  12. cout << data[index] << endl;
  13. index = next_index(index);
  14. }
  15.  
  16. }
  17.  
  18. template <class Item>
  19. void Queue<Item>::enqueue(Item entry)
  20. {
  21. rear = next_index(rear);
  22. data[rear] = entry;
  23. ++count;
  24. }
  25.  
  26. template <class Item>
  27. Item Queue<Item>::dequeue()
  28. {
  29. Item dequeued;
  30. dequeued = data[front];
  31. front = next_index(front);
  32. --count;
  33. return dequeued;
  34. }
  35.  
  36. template <class Item>
  37. int Queue<Item>::return_index()
  38. {
  39. return count;
  40. }

project6.cpp(client file)
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "queue.h"
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int get_lowest(Queue<int> [], int);
  7.  
  8. int main()
  9. {
  10. int qs_pair, maxtran, prob, dur, seed;
  11. int count(0); // The number of customers served
  12. int entry_time(0); // When each served customer arrived
  13. int wait_sum(0); // Sum of waiting times
  14.  
  15.  
  16. cout << "how many queue/server pair(s)? ";
  17. cin >> qs_pair;
  18. cout << "\nthe max time for customer transaction --> ";
  19. cin >> maxtran;
  20. cout << "\nthe probability a customer will arrive --> ";
  21. cin >> prob;
  22. cout << "\nduration of simulation(at least 1) --> ";
  23. cin >> dur;
  24. cout << "\nenter an integer seed --> ";
  25. cin >> seed;
  26.  
  27. srand(seed);
  28. Queue<int> line[qs_pair]; //needs fixing
  29. int trans_time[qs_pair]; //time remaining in a transaction
  30.  
  31. for(int i = 0; i < qs_pair; i++)// needs fixing
  32. {
  33. trans_time[i] = 0;
  34. }
  35.  
  36. int lowest;
  37. int trans_time_c; // trans_time counter
  38.  
  39. for(int time = 0; time < dur; time++)
  40. {
  41. cout << "Tick " << time << " " << endl;
  42.  
  43. if(rand() % 100 < prob)
  44. {
  45.  
  46. lowest = get_lowest(line, qs_pair);
  47.  
  48. line[lowest].enqueue(time);
  49. }
  50.  
  51. //good above
  52.  
  53. for(trans_time_c = 0; trans_time_c < qs_pair; trans_time_c++)
  54. {
  55. if(trans_time[trans_time_c] == 0)
  56. {
  57. if(line[trans_time_c].return_index() != 0)
  58. {
  59. entry_time = line[trans_time_c].dequeue();
  60. wait_sum += (time - entry_time);
  61. ++count;
  62. trans_time[trans_time_c] = (rand() % maxtran) + 1;
  63. }
  64. }
  65. else
  66. {
  67. trans_time[trans_time_c] -= 1;
  68. }
  69.  
  70.  
  71. }
  72. for(int i = 0; i < qs_pair; i++)//needs fixing
  73. {
  74. cout << "line " << i << " trans time " << trans_time[i] << endl;
  75. }
  76. cout << endl;
  77.  
  78. }
  79.  
  80.  
  81. }
  82.  
  83. int get_lowest(Queue<int> line[], int qs_pair)
  84. {
  85. int temp = line[0].return_index();
  86. int temp_i = 0;
  87.  
  88. for(int i = 0; i < qs_pair; i++)
  89. {
  90. if(line[i].return_index() == 0)
  91. return i;
  92. else if((i == qs_pair - 1) && line[qs_pair - 1].return_index() < temp)
  93. {
  94. temp = line[qs_pair - 1].return_index();
  95. temp_i = qs_pair - 1;
  96. }
  97. else
  98. {
  99.  
  100. if(line[i].return_index() > temp)
  101. {
  102.  
  103. }
  104. else
  105. {
  106. temp = line[i].return_index();
  107. temp_i = i;
  108. }
  109. }
  110. }
  111. return temp_i;
  112. }
Similar Threads
Reputation Points: 46
Solved Threads: 1
Junior Poster in Training
homeryansta is offline Offline
87 posts
since Jan 2009
Mar 22nd, 2009
0

Re: where is my error coming from?

anyone?
Reputation Points: 46
Solved Threads: 1
Junior Poster in Training
homeryansta is offline Offline
87 posts
since Jan 2009
Mar 23rd, 2009
1

Re: where is my error coming from?

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
Reputation Points: 15
Solved Threads: 0
Newbie Poster
xkey is offline Offline
13 posts
since Mar 2009
Mar 23rd, 2009
0

Re: where is my error coming from?

Click to Expand / Collapse  Quote originally posted by xkey ...
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!
Reputation Points: 46
Solved Threads: 1
Junior Poster in Training
homeryansta is offline Offline
87 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: repeated output
Next Thread in C++ Forum Timeline: Towers of Hanoi Recursive





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC