-4

I create a class in Vec.h like this:

#include <VECTOR>
using namespace std;
template<class T>
class Vec:public vector<T>  
{
public:
    Vec():vector<T>(){}
    Vec(int s):vector<T>(s){}
    T& operator[](int i){return at(i);}
    const T& operator[](int i) const {return at(i);}
    ~Vec();
};

in the main.cpp:

int main(int argc, char* argv[])
{
    Vec<int> test(2);
    return 0;
}

but when I compile them,it show:

error LNK2001: unresolved external symbol "public: __thiscall Vec<int>::~Vec<int>        (void)" (??1?$Vec@H@@QAE@XZ)
Debug/CPlusTest.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

My compilation toos is VC6.0. How can I do?

Update: Sorry! I'd not copy the entire "main.cpp", it should be that:

#include <string>
#include <IOSTREAM>
#include "Vec.h"
using namespace std;
int main(int argc, char* argv[])
{
    Vec<int> test(2);
    return 0;
}

and now the error is because I dit not realize "~Vec();" so,modify this Vec.h like this:

~Vec(){}

Compiled successfully!

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
yeguo
  • 65
  • 1
  • 7
  • 1
    A) what is header `VECTOR`? B) Why don't you include `Vec.h` in `main.cpp`? C) You need an implementation for `~Vec()` – juanchopanza Dec 18 '13 at 07:05
  • 1
    Add an implementation of `~Vec()`. And once you get that working, remove `using namespace std` from your header file. `using` declarations in a header file are bad juju. – Joe Z Dec 18 '13 at 07:07

1 Answers1

3

#include <VECTOR>

I'm assuming you meant to include #include <vector>.

Unresolved external ? You did not implement ~Vec(void); only a declaration exists. Please implement the destructor or simply do not declare it.

Also for a better style I would recommend deleting the using namespace std; and use std::vector<T> instead.

Another style issue is your return 0; in the main function. Please use EXIT_SUCCESS instead.


To your edit: Told you a implementation is needed. Also you include your headers very strange.
#include <IOSTREAM>
What's this ? Please use the usual style form: #include <iostream>. The include may work, but it looks very strange.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53