So I have an assignment for my cs2 class and I have to learn how to use templates. I believe I am doing everything correctly, but I get an error when I try to link my files together. First, here are my source files:
safearray.h

Code:

#ifndef __SAFEARRAY_H
#define __SAFEARRAY_H

template <class Type>
class safeArray
{
   public:
      Type & operator[](int);
      safeArray(int, int);
      int ubound();
      int lbound();
   
   
   private:
      int ub;
      int lb;
      Type *array;
};

#endif //__SAFEARRAY_H


safearray.cpp

Code:

#include "safearray.h"
#include <iostream>

using namespace std;

template<class Type>
int safeArray<Type>::ubound()
{
   return ub;
}

template<class Type>
int safeArray<Type>::lbound()
{
   return lb;
}

template<class Type>
Type & safeArray<Type>:: operator [](int index)
{
   if(index < lb || index > ub)
      cerr << "Out of range subscript used";
      return array[index-lb];
}

template<class Type>
safeArray<Type>::safeArray(int lower_bound, int upper_bound)
{
   if(lower_bound > upper_bound)
   {
      ub = lower_bound;
      lb = upper_bound;
   } else {
      lb = lower_bound;
      ub = upper_bound;
   }
   
   array = new Type [ub - lb + 1];
}


main.cpp

Code:

#include "safearray.h"
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
   safeArray<int> a(0,5);
   for (int i = 0; i < 5; i++)
   {
      a[i] = i;
   }
   
   for(int i = 0; i < 5; i++)
   {
      cout << a[i];
   }
}


Alright so at the terminal i type:
1)g++ main.cpp -c
2)g++ safearray.cpp -c
3)g++ main.o safearray.o -o SafeArray

Steps 1 and two work, but when I try to link the files in step three i get the following errors:

main.o: In function `main':
main.cpp:(.text+0x21): undefined reference to `safeArray<int>::safeArray(int, int)'
main.cpp:(.text+0x3b): undefined reference to `safeArray<int>::operator[](int)'
main.cpp:(.text+0x69): undefined reference to `safeArray<int>::operator[](int)'
collect2: ld returned 1 exit status


And I can't seem to figure out what they mean. What could I be doing wrong to get the errors? I tried asking the professor but he won't answer questions about it because that is the purpose of homework(To make us learn it).

Edit: One problem was solved with the overloaded operator, it was returning an int, when the array could be any kind of value depending on how it was called. Then i still ran into the issue of undefined reference, and after about two hours of googling templates, I found an answer that solved my issue. I had to merge safearray.h and safearray.cpp into the same file. I can't remember what the site was, but it said most compilers don't link having the function declarations split from the class declaration.
Then shouldn't you not be asking us either? Very Happy For what it's worth, that's a simple mistake, and the problem is in the last line of you three make'ing lines.
KermMartian wrote:
... For what it's worth, that's a simple mistake, and the problem is in the last line of you three make'ing lines.


Your post is a little unclear, what do you mean?
I read it as him talking about
Quote:
1)g++ main.cpp -c
2)g++ safearray.cpp -c
3)g++ main.o safearray.o -o SafeArray
So I was thinking it was something to do with the way you are Make'ing your files. I, unfortunately, have no idea how you would fix this :/
_player1537 wrote:
I read it as him talking about
Quote:
1)g++ main.cpp -c
2)g++ safearray.cpp -c
3)g++ main.o safearray.o -o SafeArray
So I was thinking it was something to do with the way you are Make'ing your files. I, unfortunately, have no idea how you would fix this :/
Correct, I was giving you a hint about how to fix your problems. Smile
Ah okay, not sure what I could be doing wrong with the linking, but I did get the issue fixed.
lafferjm wrote:
Ah okay, not sure what I could be doing wrong with the linking, but I did get the issue fixed.
Well, what did you do to fix it? Smile
I edited my first post yesterday with the fix, but here it is Smile

One problem was solved with the overloaded operator, it was returning an int, when the array could be any kind of value depending on how it was called. Then i still ran into the issue of undefined reference, and after about two hours of googling templates, I found an answer that solved my issue. I had to merge safearray.h and safearray.cpp into the same file. I can't remember what the site was, but it said most compilers don't link having the function declarations split from the class declaration.
Excellent, that's more or less what I would have suggested (at the latter; I didn't notice the former). Good job!
Well this program is finished, the documentation is done, and it is ready to be turned in on Monday. Now to relax and refresh my mind so that I can finish my crypto program tomorrow which is also due Monday. Then I can get cracking on my other three programming assignments due next Monday Smile.
Congratulations! I hope you'll be sharing information about your crypto program with us; I always enjoy a bit of cryptographic programming.
Its really simple, all it does is use frequency analysis on the affine cipher.
Sounds like fun! I've played around with other people's programs to do that, but I'm afraid I've never written my own. I think I'd enjoy it. Smile This also is in C++?
Yeah c++
KermMartian wrote:
Congratulations! I hope you'll be sharing information about your crypto program with us; I always enjoy a bit of cryptographic programming.


This program was turned in and I am waiting for it to be graded. Once it is graded I will go ahead and post the source for it. As it stands, when I first wrote it I got about 250 possible decryptions. The final one that I turned in reduced the possibilities to 4-8. These are only with the given ciphertexts though. The professor has a secret set that he is going to be using to grade the program.
Ah, I hope it goes well, then! Good luck to you on that.
Well i got my grade back and it was a 45/50. As it turns out I made a mistake when calculating the frequency of t,a,o,i,n,s,h, and r. But in other news I got first place in the contest, because I returned the least amount of possible plain texts.
lafferjm wrote:
Well i got my grade back and it was a 45/50. As it turns out I made a mistake when calculating the frequency of t,a,o,i,n,s,h, and r. But in other news I got first place in the contest, because I returned the least amount of possible plain texts.
Congrats on the win, bummer about the frequencies. Is it not etaonrish...?
Sort of. E is 12% or something like that and the taoinshr has a different frequency.
lafferjm wrote:
Sort of. E is 12% or something like that and the taoinshr has a different frequency.
I think it depends what kind of corpus you're working from. Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement