By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Error: "Unresolved external symbol"

P: 5
Hi Everyone, I'm having a problem compiling useing the VS2005 .NET

I need help to resolve those error, I try to create a new project it doesn't help
any suggestion?

I got the following errors:


vector_main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::~Vector<int>(void)" (??1?$Vector@H@@QAE@XZ) referenced in function _main

vector_main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Vector<int>::setValue(int)" (?setValue@?$Vector@H@@QAEXH@Z) referenced in function _main

vector_main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::Vector<int>(int,int)" (??0?$Vector@H@@QAE@HH@Z) referenced in function _main

This is the main function

Expand|Select|Wrap|Line Numbers
  1. /********************
  2. * File: vector_main *
  3. ********************/
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include "Vector.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void main()
  13. {
  14.     Vector<int> v1;    
  15.     v1.setValue(5);
  16. }
  17.  
  18.  
This is the class declaration Vector.h

Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef _VECTOR_H
  3. #define _VECTOR_H
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. template<class T = int>
  9. class Vector
  10. {
  11. private:
  12.     // Data members
  13.     T *m_data;
  14.     int m_size;
  15.  
  16.     // Private functions
  17.     void reset() { m_size = 0; m_data = NULL; }
  18.     void allocate(int size) { m_size = size; m_data = new T[m_size]; } 
  19.     void copy(const T v[]);
  20.  
  21. public:
  22.  
  23.     //Constructors
  24.     explicit Vector(int size = 0, T intVal = T());
  25.  
  26.     Vector(const Vector& v);
  27.  
  28.  
  29.     //Distructor
  30.     ~Vector();
  31.  
  32.     //Public functions
  33.     int getSize() { return m_size; }
  34.  
  35.     void setValue(T val);
  36.  
  37.     //Operators
  38.     Vector& operator=(const Vector &v);
  39.  
  40.     T& operator[](int i);
  41.  
  42.     const T& operator[](int i)const;
  43.  
  44.     //Input/Output (I/O)
  45.     void print(ostream& os=cout)const;
  46.     void read(istream& is);
  47. };
  48.  
  49. #endif
  50.  
and this is the impementation file Vector.cc
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*********************
  3. * File: Vector.cpp *
  4. *********************/
  5.  
  6. #include <iostream>
  7. #include "Vector.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. template<class T>
  13. Vector<T>::Vector(int size = 0, T intVal = T())
  14. {
  15.     if (size != 0)
  16.     {
  17.         allocate(size);
  18.         setValue(intVal);
  19.     }
  20.     else
  21.         reset();
  22. }
  23.  
  24. template<class T>
  25. Vector<T>::Vector(const Vector<T> &v)
  26. {
  27.     allocate(v.getSize());
  28.     copy(v.m_data);
  29. }
  30.  
  31. template<class T>
  32. Vector<T>::~Vector()
  33. {
  34.     delete[] m_data;
  35.     reset();
  36. }
  37.  
  38. template<class T>
  39. void Vector<T>::setValue(T val)
  40. {
  41.     for (int i = 0; i < m_size; i++)
  42.         m_data[i] = val;
  43. }
  44.  
  45. template<class T>
  46. Vector<T>& Vector<T>::operator =(const Vector<T> &v)
  47. {
  48.     if (this != &v)
  49.     {
  50.         delete[] m_data;
  51.         allocate(v.getSize());
  52.         copy(v.m_data);
  53.     }
  54.     return *this;
  55. }
  56.  
  57. template<class T>
  58. T& Vector<T>::operator [](int i)
  59. {
  60.     return m_data[i];
  61. }
  62.  
  63. template<class T>
  64. const T& Vector<T>::operator [](int i) const
  65. {
  66.     return m_data[i];
  67. }
  68.  
  69. template<class T>
  70. void Vector<T>::print(ostream &os = cout) const
  71. {
  72.     for (int i = 0; i < m_data; i++)
  73.         os<<m_data[i]<<" ";
  74. }
  75.  
  76. template<class T>
  77. void Vector<T>::read(istream &is)
  78. {
  79.     for (int i = 0; i < m_data; i++)
  80.         is>>m_data[i];
  81. }
  82.  
  83. template<class T>
  84. void Vector<T>::copy(const T v[])
  85. {
  86.     for (int i = 0; i < m_size; i++)
  87.             m_data[i] = v[i]; 
  88. }
  89.  
  90.  
  91.  
Sep 7 '07 #1
Share this Question
Share on Google+
7 Replies


P: 5
Please, I need your help...
anyone fimilar with this problem and knows what could be the reason for such an error

-------------------------------------------------------------------------------------------------------------------

Hi Everyone, I'm having a problem compiling useing the VS2005 .NET

I need help to resolve those error, I try to create a new project it doesn't help
any suggestion?

I got the following errors:


vector_main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::~Vector<int>(void)" (??1?$Vector@H@@QAE@XZ) referenced in function _main

vector_main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Vector<int>::setValue(int)" (?setValue@?$Vector@H@@QAEXH@Z) referenced in function _main

vector_main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::Vector<int>(int,int)" (??0?$Vector@H@@QAE@HH@Z) referenced in function _main

This is the main function

Expand|Select|Wrap|Line Numbers
  1. /********************
  2. * File: vector_main *
  3. ********************/
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include "Vector.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void main()
  13. {
  14.     Vector<int> v1;    
  15.     v1.setValue(5);
  16. }
  17.  
  18.  
This is the class declaration Vector.h

Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef _VECTOR_H
  3. #define _VECTOR_H
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. template<class T = int>
  9. class Vector
  10. {
  11. private:
  12.     // Data members
  13.     T *m_data;
  14.     int m_size;
  15.  
  16.     // Private functions
  17.     void reset() { m_size = 0; m_data = NULL; }
  18.     void allocate(int size) { m_size = size; m_data = new T[m_size]; } 
  19.     void copy(const T v[]);
  20.  
  21. public:
  22.  
  23.     //Constructors
  24.     explicit Vector(int size = 0, T intVal = T());
  25.  
  26.     Vector(const Vector& v);
  27.  
  28.  
  29.     //Distructor
  30.     ~Vector();
  31.  
  32.     //Public functions
  33.     int getSize() { return m_size; }
  34.  
  35.     void setValue(T val);
  36.  
  37.     //Operators
  38.     Vector& operator=(const Vector &v);
  39.  
  40.     T& operator[](int i);
  41.  
  42.     const T& operator[](int i)const;
  43.  
  44.     //Input/Output (I/O)
  45.     void print(ostream& os=cout)const;
  46.     void read(istream& is);
  47. };
  48.  
  49. #endif
  50.  
and this is the impementation file Vector.cc
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*********************
  3. * File: Vector.cpp *
  4. *********************/
  5.  
  6. #include <iostream>
  7. #include "Vector.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. template<class T>
  13. Vector<T>::Vector(int size = 0, T intVal = T())
  14. {
  15.     if (size != 0)
  16.     {
  17.         allocate(size);
  18.         setValue(intVal);
  19.     }
  20.     else
  21.         reset();
  22. }
  23.  
  24. template<class T>
  25. Vector<T>::Vector(const Vector<T> &v)
  26. {
  27.     allocate(v.getSize());
  28.     copy(v.m_data);
  29. }
  30.  
  31. template<class T>
  32. Vector<T>::~Vector()
  33. {
  34.     delete[] m_data;
  35.     reset();
  36. }
  37.  
  38. template<class T>
  39. void Vector<T>::setValue(T val)
  40. {
  41.     for (int i = 0; i < m_size; i++)
  42.         m_data[i] = val;
  43. }
  44.  
  45. template<class T>
  46. Vector<T>& Vector<T>::operator =(const Vector<T> &v)
  47. {
  48.     if (this != &v)
  49.     {
  50.         delete[] m_data;
  51.         allocate(v.getSize());
  52.         copy(v.m_data);
  53.     }
  54.     return *this;
  55. }
  56.  
  57. template<class T>
  58. T& Vector<T>::operator [](int i)
  59. {
  60.     return m_data[i];
  61. }
  62.  
  63. template<class T>
  64. const T& Vector<T>::operator [](int i) const
  65. {
  66.     return m_data[i];
  67. }
  68.  
  69. template<class T>
  70. void Vector<T>::print(ostream &os = cout) const
  71. {
  72.     for (int i = 0; i < m_data; i++)
  73.         os<<m_data[i]<<" ";
  74. }
  75.  
  76. template<class T>
  77. void Vector<T>::read(istream &is)
  78. {
  79.     for (int i = 0; i < m_data; i++)
  80.         is>>m_data[i];
  81. }
  82.  
  83. template<class T>
  84. void Vector<T>::copy(const T v[])
  85. {
  86.     for (int i = 0; i < m_size; i++)
  87.             m_data[i] = v[i]; 
  88. }
  89.  
  90.  
  91.  
Sep 7 '07 #2

RRick
Expert 100+
P: 463
What compiler are you using?
Sep 7 '07 #3

RRick
Expert 100+
P: 463
Oops. Sorry, I missed the compiler definition in your message
Sep 7 '07 #4

P: 5
Oops. Sorry, I missed the compiler definition in your message

[TEXT=JustBeSimple]
Hi, I just notice that if I implenet all in the "Vector.h" file, so its ok
so probably the problem is how I declare it in my "Vector.cpp" file
[/TEXT]
Sep 7 '07 #5

RRick
Expert 100+
P: 463
That was the reason for my original question. Some compilers (GNU g++) must have all the "template" code in the header file, no exceptions.

I thought windows didn't have this problem. Maybe somebody else knows for certain.

Just a simple stupid question: Did you make sure the vector.o file was included in the link? Once again, I thought VS took care of this.
Sep 8 '07 #6

P: 5
That was the reason for my original question. Some compilers (GNU g++) must have all the "template" code in the header file, no exceptions.

I thought windows didn't have this problem. Maybe somebody else knows for certain.

Just a simple stupid question: Did you make sure the vector.o file was included in the link? Once again, I thought VS took care of this.
Where can see if the "Vector.o" file was included in the link?
Or can I set it?
Sep 8 '07 #7

P: 5
Where can see if the "Vector.o" file was included in the link?
Or can I set it?
Ok, its normal behavor you can look at this link for the solution and/or the reason it happening


Thanks any way
Sep 8 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.