M
Mark
Sorry for creating such a newbish topic, but I just can't seem to
figure out what the problem is here.
// main.cpp
#include <cstdlib>
#include <iostream>
#include "Vector.h"
using namespace std;
int main(int argc, char *argv[])
{
Vector<int> v1;
system("PAUSE");
return EXIT_SUCCESS;
}
// Vector.h
#ifndef VECTOR_H
#define VECTOR_H
class IndexOutofBounds {
};
template <class T>
class Vector {
public:
Vector();
Vector(int size);
Vector(const Vector& r);// COPY
virtual ~Vector();
T& operator[](int index) throw(IndexOutofBounds);
int size();
int inflate(int addSize);
private:
T *m_pElements;
int m_nSize;
};
#endif
// Vector.cpp
#include "Vector.h"
// consturctors
template <class T>
Vector<T>::Vector() : m_nSize(0), m_pElements(0) {}
template <class T>
Vector<T>::Vector(int size) : m_nSize(size) {
m_pElements = new T[m_nSize];
}
template <class T>
Vector<T>::Vector(const Vector& r) {
m_nSize = r.m_nSize;
m_pElements = new T[m_nSize];
for(int i=0; i<m_nSize; i++)
m_pElements = r.m_pElements;
}
// destructor
template <class T>
Vector<T>::~Vector() {
delete[] m_pElements;
// ... plus rest of functions
compile log:
g++.exe -c main.cpp -o main.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe -c Vector.cpp -o Vector.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o Vector.o -o "Project1.exe" -L"C:/Dev-Cpp/lib"
main.o(.text+0x16a):main.cpp: undefined reference to
`Vector<int>::Vector()'
main.o(.text+0x175):main.cpp: undefined reference to
`Vector<int>::~Vector()'
collect2: ld returned 1 exit status
make.exe: *** [Project1.exe] Error 1
Execution terminated
I don't understand why I'm getting the linker error... Vector.cpp is
compiled, everything *should* be defined, no?
figure out what the problem is here.
// main.cpp
#include <cstdlib>
#include <iostream>
#include "Vector.h"
using namespace std;
int main(int argc, char *argv[])
{
Vector<int> v1;
system("PAUSE");
return EXIT_SUCCESS;
}
// Vector.h
#ifndef VECTOR_H
#define VECTOR_H
class IndexOutofBounds {
};
template <class T>
class Vector {
public:
Vector();
Vector(int size);
Vector(const Vector& r);// COPY
virtual ~Vector();
T& operator[](int index) throw(IndexOutofBounds);
int size();
int inflate(int addSize);
private:
T *m_pElements;
int m_nSize;
};
#endif
// Vector.cpp
#include "Vector.h"
// consturctors
template <class T>
Vector<T>::Vector() : m_nSize(0), m_pElements(0) {}
template <class T>
Vector<T>::Vector(int size) : m_nSize(size) {
m_pElements = new T[m_nSize];
}
template <class T>
Vector<T>::Vector(const Vector& r) {
m_nSize = r.m_nSize;
m_pElements = new T[m_nSize];
for(int i=0; i<m_nSize; i++)
m_pElements = r.m_pElements;
}
// destructor
template <class T>
Vector<T>::~Vector() {
delete[] m_pElements;
// ... plus rest of functions
compile log:
g++.exe -c main.cpp -o main.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe -c Vector.cpp -o Vector.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o Vector.o -o "Project1.exe" -L"C:/Dev-Cpp/lib"
main.o(.text+0x16a):main.cpp: undefined reference to
`Vector<int>::Vector()'
main.o(.text+0x175):main.cpp: undefined reference to
`Vector<int>::~Vector()'
collect2: ld returned 1 exit status
make.exe: *** [Project1.exe] Error 1
Execution terminated
I don't understand why I'm getting the linker error... Vector.cpp is
compiled, everything *should* be defined, no?