1

from building the source code I get this error, I am not sure what it means from my previous post I got helped and I got further and this is what I have until now, thanks for any help,

main.cpp:31:2: error: no match for 'operator<<' in 'os << (& q)->Queue::PrintQ()'

template<class T> class Queue;
template<class T> ostream& operator<<(ostream& os, Queue<T>& q);

template<class T>
class Queue{

protected:
    vector<T> theQ;
public:
    void Push(T item);
    T pop();
    void ReadAnItem();
    void PrintQ();
    friend ostream& operator<< <T>(ostream& os, Queue<T>& q);
};

template<class T>
ostream& operator<< (ostream& os, Queue<T>& q){
    os << q.PrintQ();
    return os;
}

template<class T>
void Queue<T>::Push(T item){
    theQ.push_back(item);
}

template<class T>
T pop(){
}

template<class T>
void Queue<T>::ReadAnItem(){
    T item;
    cout << "Enter the data please: " << endl;
    cin >> item;
    Push(item);
}

template<class T>
void Queue<T>::PrintQ(){
    cout << "The content of the array is as follows: " << endl;
        for (int i=0; i < theQ.size(); i++){
            cout << theQ[i];
            cout<< endl;
        }

}

class Employee{

protected:
    long empId;
    string empName;
    string email;
public:
    Employee(){}
    Employee(long i, string n){
        empName = n,
        empId =i;
        email = "Unknown";
    }
    friend ostream& operator<<(ostream& os, Employee& e){
        os << e;
        return os;
    }
};

class Student{

protected:
    long stId;
    int year;
    string email;
    string schoolName;
public:
    Student(){}
    Student(long i, int y, string sn){
        stId = i;
        year =y;
        email = "Unknown";
        schoolName=sn;
    }
    friend ostream& operator<<(ostream& os, Student& s){
        os << s;
        return os;
    }
};

int main(){

        Queue<Student> s;
        s.Push(Student(300982, 21, "Andrey"));
        cout << s;
        return 0;
}
Andrey Arias
  • 57
  • 1
  • 10

2 Answers2

1

This is a problem:

os << q.PrintQ();

PrintQ() is void. You are trying to stream the result of a call to a void function to std::cout. A possible solution would be the following:

template<class T>
void Queue<T>::PrintQ(std::ostream& o) const {
  o << "The content of the array is as follows: " << endl;
    for (int i=0; i < theQ.size(); i++){
        o << theQ[i];
        o << endl;
  }
}

and then

template<class T>
ostream& operator<< (ostream& os, const Queue<T>& q){
    q.PrintQ(os);
    return os;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • now I get this error, I edited the code with your suggestion and when I tried to print ion main I get an error...how should I call the function I tried cout << s; and s.PrintQ(cout); main.cpp:107: undefined reference to `std::ostream& operator<< (std::ostream&, Queue&) – Andrey Arias Nov 25 '13 at 22:12
  • @AndreyArias You need to fix the implementation of `ostream& operator<<(ostream& os, Student& s)`. Currently it is calling itself. – juanchopanza Nov 26 '13 at 06:40
0

Some small points:

  1. theQ.size() returns an unsigned integer, so technically it should be unsigned int.
  2. Testing theQ.size() on every loop is inefficient, Initialise a variable to theQ.size() and use that to test.
  3. Better still is to use iterators to run through your vector.
  4. Best is to you the STL and use an algorithm:

    copy( theQ.begin(), theQ.end(), ostream_iterator<T&>(os, "\n"));

  5. Your operator<< functions have a problem: when you write os << s you are telling a student object to stream itself. As a Student object has absolutely no idea how to do that the program will create a segment violation. Solve this by replacing os << s with something like os << "Id: " << s.stId << "\tYear: " << s.year << "\temail: " << s.email << "\tschoolname: " << s.schoolName; You can also write a stream member function and keep the os << s; line.

Hope this was of any help.

Good luck

Alwin