help please

This is a discussion on help please within the C++ Programming forums, part of the General Programming Boards category; i am getting the following error in my code. I am trying to convert a queue class to a template. ...

  1. #1
    Unregistered
    Guest

    help please

    i am getting the following error in my code. I am trying to convert a queue class to a template. the compiler doesn't like my operator overloading. I get the following error:

    queue.h(20) : error C2679: binary '<<' : no operator defined which
    takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

    queue.h(16) : while compiling class-template member function
    'class ostream &__cdecl Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >:perator <<(class ostream &,class Queue<class std::basic_string<char,struct std::char_traits<char>,
    class std::allocator<char> > >)'
    Error executing cl.exe.

    main.obj - 1 error(s), 1 warning(s)


    here's my code:

    //main.cpp

    #include "queue.h"

    #include <string>
    using std::string;

    int main() {
    Queue <int> q;
    q.enqueue(123);
    q.enqueue(42);
    cout << q;
    q.dequeue();
    cout << q;

    Queue <string> s;
    s.enqueue("student1");
    s.enqueue("student2");
    cout << s;
    s.dequeue();
    cout << s;
    return 0;

    }
    --------------------------------
    //queue.h

    #ifndef _QUEUE_H
    #define _QUEUE_H

    #include <iostream.h>
    using std::cout;
    using std::endl;

    template <class T>
    class Queue {

    friend ostream& operator<<(ostream& out, class <T> q) {
    out << "Front" << endl;
    for (int i = q.myFront; i != q.myNextFreePosition; q.incr(i)) {
    out << q.myData[i] <<"," ;
    }
    out << "END" << endl;
    return out;
    }

    public:
    T dequeue() {
    T retval = myData [myFront];
    return retval;
    } //removes element from front

    void enqueue(T item){
    myData[myNextFreePosition] = item;
    incr (myNextFreePosition);
    } // adds element to back

    bool empty() {
    return (myNextFreePosition == myFront);
    }

    T front (){
    return myData[myFront];
    } //returns element from front

    Queue(){
    myNextFreePosition = 0;
    myFront = 0;
    } //constructor

    private:
    void incr (int &pos) {
    pos = (pos + 1) % 1300;
    }//increment index of array

    T myData[1300]; // array of data
    int myNextFreePosition; // next free position
    int myFront; //front of queue
    };
    #endif


    my program does not like the <string> class in main.cpp. if i replace <string> with <char*> the program works. however my lab assignment is to use <string>. Any ideas on why i am getting this error. been looking at this program too long now to see what's wrong. sure it's obvious to everyone but me. thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I get the following error:
    Old C++ didn't support namespaces, so when you use iostream.h you can't declare any using statements for iostream objects. To solve your current problem, change this in your queue.h file:

    #include <iostream.h>
    using std::cout;
    using std::endl;

    to this:

    #include <iostream>
    using std::cout;
    using std::endl;

    That will fix those first two errors, but you have quite a few other problems with your code. I'll leave debugging that to you unless you get really stuck.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Try reversing the order of your includes and make sure the using std statement is also ahead of the other include.

  4. #4
    Unregistered
    Guest
    prelude & golfinguy4 - thanks for the tips.

    prelude - i made the changes you suggested on now i get 28 errors. here's what i get:

    queue.h(23) : error C2143: syntax error : missing ';' before '&'
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2433: 'ostream' : 'friend' not permitted on data declarations
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2501: 'ostream' : missing storage-class or type specifiers
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2244: 'ostream' : unable to resolve function overload
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2061: syntax error : identifier 'ostream'
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2501: '<<' : missing storage-class or type specifiers
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2805: binary 'operator <<' has too few parameters
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2333: '<<' : error in function declaration; skipping function body
    queue.h(64) : see reference to class template instantiation 'Queue<T>' being compiled
    queue.h(23) : error C2143: syntax error : missing ';' before '&'
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2433: 'ostream' : 'friend' not permitted on data declarations
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2501: 'ostream' : missing storage-class or type specifiers
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2244: 'ostream' : unable to resolve function overload
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2061: syntax error : identifier 'ostream'
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2501: '<<' : missing storage-class or type specifiers
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2805: binary 'operator <<' has too few parameters
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2333: '<<' : error in function declaration; skipping function body
    main.cpp(13) : see reference to class template instantiation 'Queue<int>' being compiled
    queue.h(23) : error C2143: syntax error : missing ';' before '&'
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2433: 'ostream' : 'friend' not permitted on data declarations
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2501: 'ostream' : missing storage-class or type specifiers
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2244: 'ostream' : unable to resolve function overload
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2061: syntax error : identifier 'ostream'
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2501: '<<' : missing storage-class or type specifiers
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2805: binary 'operator <<' has too few parameters
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    queue.h(23) : error C2333: '<<' : error in function declaration; skipping function body
    main.cpp(14) : see reference to class template instantiation 'Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' being compiled
    main.cpp(17) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Queue<int>' (or there is no acceptable conversion)
    main.cpp(19) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Queue<int>' (or there is no acceptable conversion)
    main.cpp(24) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' (or there
    is no acceptable conversion)
    main.cpp(26) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Queue<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' (or there
    is no acceptable conversion)
    Error executing cl.exe.

    main.obj - 28 error(s), 0 warning(s)


    i know you said there were other problems with my code, but i cannot figure out what they are. i made a few changes, but i seem to be getting nowhere. here are the changes i made - not very many:

    //queue2.h

    #ifndef _QUEUE_H
    #define _QUEUE_H

    #include <iostream>

    using std::cout;
    using std::endl;

    template <class T>
    class Queue {

    friend ostream& operator<<(ostream& out, Queue q) {
    out << "Front" << endl;
    for (int i = q.myFront; i != q.myNextFreePosition; q.incr(i)) {
    out << q.myData[i] <<"," ;
    }
    out << "END" << endl;
    return out;
    }

    public:
    T dequeue() {
    T retval = myData [myFront];
    return retval;
    } //removes element from front

    void enqueue(T item){
    myData[myNextFreePosition] = item;
    incr (myNextFreePosition);
    } // adds element to back

    bool empty() {
    return (myNextFreePosition == myFront);
    }

    T front (){
    return myData[myFront];
    } //returns element from front

    Queue(){
    myNextFreePosition = 0;
    myFront = 0;
    } //constructor

    private:
    void incr (int &pos) {
    pos = (pos + 1) % 1300;
    }//increment index of array

    T myData[1300]; // array of data
    int myNextFreePosition; // next free position
    int myFront; //front of queue
    };
    #endif
    -----------------
    //main.cpp

    #include "queue.h"
    #include <string>
    using std::string;

    int main() {
    Queue <int> q;
    Queue <string> s;
    q.enqueue(123);
    q.enqueue(42);
    cout << q;
    q.dequeue();
    cout << q;

    //Queue <string> s;
    s.enqueue("student1");
    s.enqueue("student2");
    cout << s;
    s.dequeue();
    cout << s;
    return 0;

    }

    believe it or not the following code i've posted compiles on the linux workstation i telnet to. however the errors i am getting are from trying to run this in visual c++ ver 6. if you could give me a hint on what is wrong with my code it would be greatly appreciated. i know it's something very simple but i am starting to go nuts looking at this code. any help would be greatly appreciated. thanks in advance.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Replace your queue.h with this. It compiles and links but i didn't check to see if the logic is sound.
    Code:
    #ifndef _QUEUE_H 
    #define _QUEUE_H 
    
    #include <iostream> 
    
    using std::cout; 
    using std::endl; 
    
    template <class T> 
    class Queue { 
      friend std::ostream& operator<< ( std::ostream& out, Queue q ) { 
        out << "Front" << endl; 
        for (int i = q.myFront; i != q.myNextFreePosition; q.incr(i)) { 
          out << q.myData[i] <<"," ; 
        } 
        out << "END" << endl; 
        return out; 
      } 
    public: 
      T dequeue() { 
        T retval = myData [myFront]; 
        return retval; 
      } //removes element from front 
    
      void enqueue(T item){ 
        myData[myNextFreePosition] = item; 
        incr (myNextFreePosition); 
      } // adds element to back 
    
      bool empty() { 
        return (myNextFreePosition == myFront); 
      } 
    
      T front (){ 
        return myData[myFront]; 
      } //returns element from front 
    
      Queue(){ 
        myNextFreePosition = 0; 
        myFront = 0; 
      } //constructor 
    
    private: 
      void incr (int &pos) { 
        pos = (pos + 1) % 1300; 
      }//increment index of array 
    
      T myData[1300]; // array of data 
      int myNextFreePosition; // next free position 
      int myFront; //front of queue 
    }; 
    #endif
    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    Prelude-I changed my code to your suggestions and it works perfectly. THANK YOU!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21