-2

Ok I have updated the code:

#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

Error list: Error 1 error C2039: 'add' : is not a member of 'MyClass' c:\users\mihaibogdan\documents\visual studio 2013\projects\dashit\dashit\header.h 41

Mihai
  • 55
  • 6
  • Try using `std::ostream&` instead of `ostream&`. – phantom Apr 25 '15 at 20:23
  • 2
    First of all, *what* errors are you getting? Please edit your question to include the complete and unedited error log. Secondly, which line is line 13? Please mark it out some way, like a comment or something. – Some programmer dude Apr 25 '15 at 20:24
  • Maybe you'll like [Clang's errors](http://coliru.stacked-crooked.com/a/072554d676124b1b) better. On a side note, there are a ton of errors in here. – chris Apr 25 '15 at 20:24
  • Note that your header guards don't work, since you don't actually `#define VECTOR_H`. – BoBTFish Apr 25 '15 at 20:24
  • `vec = new T[i]` needs a semicolon after it, like your compiler is telling you. – Barry Apr 25 '15 at 20:26
  • if you use something like function(int i){ i++}; u need to place ';' even thou its a single command? – Mihai Apr 25 '15 at 20:30
  • @Mihai: Yes. Try it if you don't believe it. – Beta Apr 25 '15 at 23:48

1 Answers1

2

You should use qualified names

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

Standard C++ names are declared in name space std.

Otherwise the compiler looks up unqualified name ostream in the global name space where it is not of course declared.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335