简体   繁体   中英

Error C2679 binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

I write a program implement a tree and traverse breadth first this tree. the program include 3 files: queue.h, tree.h and main.cpp... This is queue.h

#pragma once
#include <iostream>

template <class T>
struct L1node {
    T data;
    L1node* next = NULL;
};

template <class T>
class L1queue
{
    L1node<T>* pHead, pTail;
public:
    L1queue() :pHead(NULL), pTail(NULL) {}
    ~L1queue() {
        if (pHead) {
            while (pHead->next) {
                L1node<T>* p = pHead;
                pHead = pHead->next;
                delete p;
            }
            delete pHead;
        }
        return 0;
    }
    bool isEmpty() {
        return pHead == NULL:
    }
    T& head() {
        if (isEmpty()) throw - 1;
        return pHead->data;
    }
    void enqueue(T& a) {
        L1node<T> p = new L1node<T>(a);
        if (isEmpty()) {
            pHead = pTail = p;
        }
        else {
            pTail->next = p;
            pTail = p;
        }
    }
    void dequeue() {
        if (isEmpty()) return;
        L1node<T>* p = pHead;
        pHead = pHead->next;
        delete p;
        if (pHead == NULL) pTail = NULL;   //Error here
    }
};

and this is tree.h

#include <iostream>
#include "queue.h"
using namespace std;

class treeNode {
public:
    int data;
    treeNode* left = NULL;
    treeNode* right = NULL;
};
void printLeavesBFT(treeNode* root) {
    L1queue<treeNode*> q;
    while (!q.isEmpty()) {
        treeNode* p = q.head(); cout << p->data << " ";
        q.dequeue();
        if(p->left) q.enqueue(root->left);
        if(p->right) q.enqueue(root->right);
    }
}

I meet error in dequeue function. I don't know the reason. Can you help me!

pTail is declared as a L1node<T> while you meant to declare it as a L1node<T>* . Then, pTail = NULL makes no sense.

When you write L1node<T>* pHead, pTail; , only pHead is a pointer, pTail is an object.

Replace by

L1node<T> *pHead, *pTail;

Or:

L1node<T>* pHead;
L1node<T>* pTail;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question error C2679: binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rational' (or there is no acceptable conversion) Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion) error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'RatNum' (or there is no acceptable conversion) Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM