C ++意外语法错误

时间:2015-04-25 20:21:25

标签: c++ constructor include friend ostream

好的,我已经更新了代码:

#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':不是'MyClass'的成员c:\ users \ mihaibogdan \ documents \ visual studio 2013 \ projects \ dashit \ dashit \ header.h 41

1 个答案:

答案 0 :(得分:2)

您应该使用限定名称

friend std::ostream& operator<<( std::ostream& out, const T& obj);

标准C ++名称在名称空间std中声明。

否则,编译器会在全局名称空间中查找非限定名称ostream,而当前它不会被声明。