我自定义一个MyVector,用来存储任意类型的对象,但我实现的时候出现以下错误,哪位大佬能帮帮我呀?一共三个文件:MyVector.h,MyVector.cpp和main.
cpp
1.MyVector.h
//
// Created by Howard on 2019/7/5.
//
#ifndef TEST_MYVECTOR_H
#define TEST_MYVECTOR_H
#endif //TEST_MYVECTOR_H
#include <iostream>
using namespace std;
template<typename T>
class MyVector {
friend ostream &operator << <T> (ostream &out, const MyVector<T> &obj); // 重载左移<< 右移>> 才将重载函数声明为友元函数
public:
MyVector(int size = 0); // 构造函数
MyVector(const MyVector &obj); // 拷贝构造函数
~MyVector(); // 析构函数
public:
T &operator[](int index);
MyVector &operator=(const MyVector &obj);
public:
int getlen() {
return m_len;
}
protected:
T *m_space;
int m_len;
};
2.MyVector.cpp
//
// Created by Howard on 2019/7/5.
//
#include <iostream>
using namespace std;
#include "MyVector.h"
int size = 100;
// MyVector<int> myv1(10)
template<typename T>
MyVector<T>::MyVector(int size) // 构造函数
{
m_space = new T[size];
m_len = size;
}
// MyVector<int> myv2 = myv1
template<typename T>
MyVector<T>::MyVector(const MyVector &obj) // 拷贝构造函数
{
// 根据myv1的大小分配内存
m_len = obj.m_len;
m_space = new T[size];
// cpy数据
for (int i = 0; i < m_len; i++) {
m_space[i] = obj[i];
}
}
template<typename T>
MyVector<T>::~MyVector() // 析构函数
{
if (m_space != NULL) {
delete[] m_space;
m_space = NULL;
m_len = 0;
}
}
template<typename T>
T &MyVector<T>::operator[](int index) {
return m_space[index];
}
template<typename T>
MyVector<T> &MyVector<T>::operator=(const MyVector<T> &obj) {
// 先把a2旧的内存释放掉
if (m_space != NULL) {
delete[] m_space;
m_space = NULL;
m_len = 0;
}
// 根据a1分配内存
m_len = obj.m_len;
m_space = new T[size];
// cpy数据
for (int i = 0; i < obj.m_len; i++) {
m_space[i] = obj.m_space[i];
}
return *this; // a2 = a1 返回给a2的自身
}
template<typename T>
ostream &operator<< <T>(ostream &out, const MyVector<T> &obj) {
for (int i = 0; i < obj.m_len; i++) {
out << obj.m_space[i] << " ";
}
// out << endl;
return out;
}
3.main.cpp
#include <iostream>
using namespace std;
#include "MyVector.cpp"
int main() {
MyVector<int> myv1(10);
for (int i = 0; i < myv1.getlen(); i++) {
myv1[i] = i + 1;
cout << myv1[i] << " ";
}
cout << endl;
MyVector<int> myv2 = myv1;
for (int i = 0; i < myv2.getlen(); i++) {
cout << myv2[i] << " ";
}
cout << myv2 << endl;
system("pause");
return 0;
}
运行结果:
In file included from F:\code\c++\test\main.cpp:5:
F:\code\c++\test\MyVector.cpp:76:61: error: non-class, non-variable partial specialization 'operator<< ' is not allowed
ostream &operator<< (ostream &out, const MyVector &obj) {
^
F:\code\c++\test\MyVector.cpp:76:61: error: non-class, non-variable partial specialization 'operator<< ' is not allowed
ostream &operator<< (ostream &out, const MyVector &obj) {
^
In file included from F:\code\c++\test\MyVector.cpp:9,
from F:\code\c++\test\main.cpp:5:
F:\code\c++\test\MyVector.h: In instantiation of 'class MyVector':
F:\code\c++\test\main.cpp:8:23: required from here
F:\code\c++\test\MyVector.h:16:21: error: template-id 'operator<< ' for 'std::ostream& operator<<(std::ostream&, const MyVector&)' does not match any template declaration
friend ostream &operator << (ostream &out, const MyVector &obj); // 閲嶈浇宸︾Щ<< 鍙崇Щ>> 鎵嶅皢閲嶈浇鍑芥暟澹版槑涓哄弸鍏冨嚱鏁�
^~~~~~~~~~~~~~~
In file included from D:/PROGRA~1/JETBRA~1/X86_64~1.0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from F:\code\c++\test\main.cpp:1:
D:/PROGRA~1/JETBRA~1/X86_64~1.0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:682:5: note: candidates are: 'template typename std::enable_ifstd::__and_<std::__not_<std::is_lvalue_reference<_Tp >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable::__ostream_type, const Tp&, void> >::value, typename std::_is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&)'
operator<<(_Ostream&& __os, const _Tp& __x)
^~~~~~~~
谁能告诉我,我哪个地方写错了?