LNK2019 and LNK1120 errors
Oct 22, 2008 at 4:26am UTC
Naturally, I searched for solutions about these errors, but it doesn't seem to corrolate with mine. I've been trying to get this dynamic array/vector program to run, but I either run into these two errors or run into illegal indirections. I figure there is some sort of error with passing some arguments, but I'm not really sure.
Any help is appreciated
I believe the issue to be the function on line 50-59. I always seem to have trouble with overloading.
1 2 3 4 5
1
1>Compiling...
1>Exercise_4.cpp
1>Linking...
1>Exercise_4.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class dynamicType<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$dynamicType@H@@@Z) referenced in function _main
1> : fatal error LNK1120: 1 unresolved externals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class dynamicType
{
public :
dynamicType(const T& value);
dynamicType(const dynamicType<T> & obj);
// constructor and copy constructor
~dynamicType();
// destructor
dynamicType<T> & operator = (const dynamicType<T>& rhs);
// assignment operator
T getData() const ;
// data access
friend ostream& operator << (ostream& ostr, const dynamicType<T>& obj);
// stream output
private :
T *ptr;
};
template <typename T>
dynamicType<T>::dynamicType(const T& value)
{
ptr = new T[value];
ptr = T();
}
template <typename T>
dynamicType<T>::dynamicType(const dynamicType<T> &obj)
{
int value;
value = *obj.ptr;
ptr = new T[value];
}
template <typename T>
dynamicType<T>::~dynamicType()
{
delete [] ptr;
}
template <typename T>
dynamicType<T> & dynamicType<T>::operator = (const dynamicType<T> &rhs)
{
int i;
for (i=0;i<5;i++)
ptr[i] = rhs.ptr[i];
return *this ;
}
template <typename T>
T dynamicType<T>::getData() const
{
return *ptr;
}
template <typename T>
ostream& operator << (ostream& ostr, const dynamicType<T> &obj)
{
ostr << *obj.ptr;
return ostr;
}
I'm not exactly sure what I'm doing. I've tried declaring line 56 as below, but end up with illegal indirection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
error C2100: illegal indirection
dynt.h(55) : while compiling class template member function 'dynamicType<T> &dynamicType<T>::operator =(const dynamicType<T> &)'
1> with
1> [
1> T=int
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\vector(997) : while compiling class template member function 'void std::vector<_Ty>::_Reverse(dynamicType<T> *,dynamicType<T> *)'
1> with
1> [
1> _Ty=dynamicType<int >,
1> T=int
1> ]
1>exercise_4.cpp(20) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=dynamicType<int >
1> ]
1>dynt.h(59) : error C2100: illegal indirection
Here's the main source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <vector>
#include "dynt.h"
using namespace std;
template <typename T>
dynamicType<T> sum(const vector<dynamicType<T> >& v);
int main()
{
dynamicType<int > arr[5] = {
dynamicType<int > (3), dynamicType<int > (5),
dynamicType<int > (8), dynamicType<int > (7),
dynamicType<int > (2) };
vector< dynamicType<int > > v(arr, arr+5);
cout << sum(v) << endl;
return 0;
}
template <typename T>
dynamicType<T> sum(const vector< dynamicType<T> > & v)
{
T sum = T();
int i;
for (i=0;i < v.size(); i++)
sum += v[i].getData();
return dynamicType<T> (sum);
}
Oct 23, 2008 at 1:34pm UTC
Found a few things wrong.
1. The problem with the friend function was difficult to track down - could not find an answer to it in any book - but managed to track down an answer on the internet.
So check out the new declaration for this function.
2. The program always crashed - this was due to the way the
T *ptr
variable was being set/allocated. I have made some changes.
I have checked it with type
int
and type
double
The header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class dynamicType
{
public :
dynamicType(const T& value);
dynamicType(const dynamicType<T> & obj);
// constructor and copy constructor
~dynamicType();
// destructor
dynamicType<T> & operator = (const dynamicType<T>& rhs);
// assignment operator
T getData() const ;
// data access
//NOTICE THE DECLARATION OF THIS FRIEND FUNCTION
friend ostream& operator << <>(ostream& ostr, const dynamicType<T> obj);
// stream output
private :
T *ptr;
};
template <typename T>
dynamicType<T>::dynamicType(const T& value)
{
ptr = new T(value);
}
template <typename T>
dynamicType<T>::dynamicType(const dynamicType<T> &obj)
{
T value = *(obj.ptr);
ptr = new T(value);
}
template <typename T>
dynamicType<T>::~dynamicType()
{
if (ptr)
{
delete ptr;
}
}
template <typename T>
dynamicType<T> & dynamicType<T>::operator = (const dynamicType<T> &rhs)
{
if (this != &rhs)
{
ptr = new T (*rhs.ptr) ;
}
return *this ;
}
template <typename T>
T dynamicType<T>::getData() const
{
return *ptr;
}
template <typename T>
ostream& operator << (ostream& ostr, const dynamicType<T> obj)
{
ostr << *obj.ptr;
return ostr;
}
The main file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <vector>
#include "dynt.h"
using namespace std;
template <typename T>
dynamicType<T> sum(const vector<dynamicType<T> >& v);
int main()
{
dynamicType<int > arr[] = {
dynamicType<int > (3), dynamicType<int > (5),
dynamicType<int > (8), dynamicType<int > (7),
dynamicType<int > (2) };
vector< dynamicType<int > > v(arr, arr+5);
cout << sum(v) << endl;
return 0;
}
template <typename T>
dynamicType<T> sum(const vector< dynamicType<T> > & v)
{
T sum = T(0);
int i;
for (i=0;i < v.size(); i++)
sum += v[i].getData();
return (dynamicType<T>) sum;
}
Topic archived. No new replies allowed.