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:
safearray.cpp
Code:
main.cpp
Code:
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.
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.