矢量类模板C ++

时间:2016-12-21 07:49:21

标签: c++

我不知道如何将文件HPP:class模板与不同的文件连接起来。所以我的计划没有运行。

文件HPP:

#include<iostream>
#include<vector>
using namespace std;
template <class T>
class MyVector
{
    vector <T*> ptr;
public:
    MyVector<T>();
   ~MyVector<T>();
    friend istream& operator >><> (istream &is, MyVector<T> &x);
    friend ostream& operator <<<> (ostream &os, MyVector<T> &x);
    //MyVector<T>(vector <T*> p);
    //MyVector<T>(const Myvector<T> &x);

    //T operator[];

    //vector<T> getVector();
    //void Sorting();
};
template <class T>
MyVector<T>::MyVector()
{
}
template <class T>
MyVector<T>::~MyVector()
{
}
template <class T>
istream& operator >> (istream &is, MyVector<T> &x)
{
    int n;
    cout << "Nhap n = ";
    is >> n;
    for (int i = 0; i < n; i++)
    {
        T *a = new T;
        is >> *a;
        x.ptr.push_back(a);
    }
    return is;
}
template <class T>
ostream& operator << (ostream &os, MyVector<T> &x)
{
    for (int i = 0; i < x.ptr.size(); i++)
        os << x.ptr[i] << endl;
    return os;
}

和class student.h:

#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
    string name, id;
    float mark;
public:
    Student();
    ~Student();
    friend istream& operator >> (istream&, Student&);
    friend ostream& operator <<(ostream&, Student);
};

文件Student.cpp:

#include "Student.h"



Student::Student()
{
}


Student::~Student()
{
}
istream& operator >> (istream &is, Student &x)
{
    cout << "Nhap id: ";
    is >> x.id;
    cout << "name: ";
    getline(is, x.name);
    cout << "mark: ";
    is >> x.mark;
    return is;
}
ostream& operator <<(ostream &os, Student x)
{
    os << x.id << endl << x.name <<endl<< x.mark << endl;
    return os;
}

和main.cpp:

#include"vector.hpp"
#include"Student.h"
int main()
{
    MyVector <Student> p;
    cin >> p;
    cout << p;
    return 0;
}

但是我收到了错误:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    Template    C:\Users\prope\Documents\Visual Studio 2015\Projects\GiaiDe\Template\MSVCRTD.lib(exe_main.obj)  1   
Severity    Code    Description Project File    Line    Suppression State
Error   LNK1120 1 unresolved externals  Template    C:\Users\prope\Documents\Visual Studio 2015\Projects\GiaiDe\x64\Debug\Template.exe  1   

请帮帮我!谢谢!

0 个答案:

没有答案