【发布时间】:2015-04-25 20:21:24
【问题描述】:
好的,我已经更新了代码:
#ifndef VECTOR_H
#define VECTOR_H
#include<cstdlib>
#include<vector>
#include<iostream>
using namespace std;
template < typename T>
class MyClass
{
public:
MyClass() : size(10), index(0) { vec = new T[10]; };
MyClass(int i) : size(i), index(0) { vec = new T[i]; };
friend bool Add(T i);
virtual ~MyClass();
friend ostream& operator<<(ostream& out, const T& obj);
private:
T * vec;
int size;
int index;
};
template <typename T>
virtual MyClass<T>::~MyClass()
{
delete[] vec;
}
template <typename T>
ostream& operator<<(ostream& out, const MyClass<T>& obj){
for (int i = 0; i < index; ++i)
out << obj[i] << " ";
out << endl;
}
template <typename T>
bool MyClass<T>::Add(T i){
if (size == index){
size *= 2;
realloc(vec, size);
}
vec[index++] = i;
}
#endif // !VECTOR_H
错误列表: 错误 1 错误 C2039: 'add' : is not a member of 'MyClass' c:\users\mihaibogdan\documents\visual studio 2013\projects\dashit\dashit\header.h 41
【问题讨论】:
-
尝试使用
std::ostream&而不是ostream&。 -
首先,您遇到了什么错误?请编辑您的问题以包含完整且未经编辑的错误日志。其次,第 13 行是哪一行?请以某种方式标记出来,例如评论或其他内容。
-
也许你会更喜欢Clang's errors。顺便说一句,这里有很多错误。
-
请注意,您的标头保护不起作用,因为您实际上并没有
#define VECTOR_H。 -
vec = new T[i]后面需要一个分号,就像你的编译器告诉你的那样。
标签: c++ constructor include friend ostream