首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >错误C2679二进制“<<”:没有找到任何操作符,它接受'T‘类型的右操作数(或者没有可接受的转换)

错误C2679二进制“<<”:没有找到任何操作符,它接受'T‘类型的右操作数(或者没有可接受的转换)
EN

Stack Overflow用户
提问于 2022-09-30 02:19:15
回答 1查看 132关注 0票数 0

嗨,我对c++很陌生,我的程序中出现了这个错误

代码语言:javascript
代码运行次数:0
运行
复制
   Severity Code    Description Project File    Line    Suppression State
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

我认为错误来自于我的pop()函数,但我不知道如何修复它。我用一个靠近底部的箭头指出了这条线的误差。请有人指导我如何纠正这个错误,非常感谢。

代码语言:javascript
代码运行次数:0
运行
复制
#include <iostream>
#include <iomanip>
#include "Queue.h"
#include "Queue.h"
#include "UniqueQueue.h"
#include "UniqueQueue.h"
#include "Dictionary.h"
#include "Dictionary.h"



int cout = 0; // won't compile if headers don't follow convention

int main(int argc, char** argv)
{
    std::cout << "Command Line:\n";
    std::cout << "--------------------------\n";
    for (int i = 0; i < argc; i++)
        std::cout << std::setw(3) << i + 1 << ": " << argv[i] << '\n';
    std::cout << "--------------------------\n\n";

    // Data to be used by the template classes
    long   dataSetA[]{ 22, 1, 455, 90, 455, 6, 43 };
    double dataSetB[]{ 34.556, 3333.3303, 122.55, 3333.3333, 678.54, 3333.3389 };
    sdds::Dictionary pData[]{
        sdds::Dictionary("dome", "a rounded vault forming the roof of a building or structure."),
        sdds::Dictionary("pollution", "An introduction into the environment of a substance that has harmful effect"),
        sdds::Dictionary("slide", "a structure with a smooth sloping surface for children to slide down."),
        sdds::Dictionary("complete", "having all the necessary or appropriate parts"),
        sdds::Dictionary("pollution", "An introduction into the environment of a substance that has harmful effect"),
        sdds::Dictionary("slide", "a structure with a smooth sloping surface for children to slide down."),
        sdds::Dictionary("complete", "having all the necessary or appropriate parts"),
        sdds::Dictionary("acceptable", "able to be agreed on; suitable"),
        sdds::Dictionary("dome", "a rounded vault forming the roof of a building or structure.")
    };

    

    {
        std::cout << "******************************************************\n";
        std::cout << "* Testing the UniqueQueue of Pairs                   *\n";
        std::cout << "******************************************************\n";
        sdds::Queue<sdds::Dictionary, 100>* UniqueQueueDictionary = new sdds::UniqueQueue<sdds::Dictionary>();

        for (const auto& item : pData)
            UniqueQueueDictionary->push(item);

        std::cout << "UniqueQueueue size: [" << UniqueQueueDictionary->size() << "]\n";
        UniqueQueueDictionary->display();

        std::cout << "Item [1]: [" << (*UniqueQueueDictionary)[1].getTerm() << "]["
            << (*UniqueQueueDictionary)[1].getDefinition() << "]\n";
        std::cout << "Item [4]: [" << (*UniqueQueueDictionary)[4].getTerm() << "]["
            << (*UniqueQueueDictionary)[4].getDefinition() << "]\n";
        std::cout << "Item [11]: [" << (*UniqueQueueDictionary)[11].getTerm() << "]["
            << (*UniqueQueueDictionary)[11].getDefinition() << "]\n";

        std::cout << "----------------------\n";
        std::cout << "| Testing Pop        |\n";
        std::cout << "----------------------\n\n";

        std::cout << "Popped Item: " << UniqueQueueDictionary->pop() << std::endl; --> Error line
        std::cout << "----------------------\n";
        UniqueQueueDictionary->display();


        std::cout << "******************************************************\n\n";
        delete UniqueQueueDictionary;
    }

    return cout;
}

Queue.h文件

代码语言:javascript
代码运行次数:0
运行
复制
#ifndef QUEUE_H_
#define QUEUE_H_
#include "Dictionary.h"

namespace sdds
{
    template <typename T, unsigned int CAPACITY>
    class Queue
    {
        T m_item[CAPACITY]{};
        unsigned int m_size{};
        T dummy{};

    public:
        Queue()
        {

        };
        T pop()
        {
            if (m_size > 0)
            {
                T temp = m_item[0];
                for (size_t i = 0; i < m_size - 1; i++)
                {
                    m_item[i] = m_item[i + 1];
                }
                m_size--;
                return temp;
            }
            return dummy;
        }
        size_t size()
        {
            return m_size;
        };
        std::ostream& display(std::ostream& ostr = std::cout)
        {
            ostr << "----------------------" << std::endl;
            ostr << "| Dictionary Content |" << std::endl;
            ostr << "----------------------" << std::endl;
            for (unsigned int i = 0; i < m_size; i++) {
                ostr << m_item[i] << std::endl;
            }
            ostr << "----------------------" << std::endl;
            return ostr;
        }
         virtual bool push(const T& item)
        {
            bool suc = false;
            if (m_size < CAPACITY)
            {
                m_item[m_size] = item;
                m_size++;
                suc = true;
            }
            return suc;
        };

        

       virtual T& operator[](size_t index)
        {
            if (index >= 0 && index < CAPACITY)
            {
                return m_item[index];
            }
            return dummy;
        }
       virtual ~Queue()
       {
       }
    };
    template <>
    Queue<Dictionary, 100u>::Queue()
    {
        Dictionary a("Empty Term", "Empty Substitute");
        dummy = a;
    };

}
#endif

dictionary.h

代码语言:javascript
代码运行次数:0
运行
复制
#ifndef DICTIONARY_H_
#define DICTIONARY_H_

#include <string>
namespace sdds
{
    class Dictionary
    {
        std::string m_term{};
        std::string m_definition{};
    public:
        const std::string& getTerm() const
        {
            return m_term;
        }
        const std::string& getDefinition() const
        {
            return m_definition;
        }
        Dictionary(const std::string& term, const std::string& definition) : m_term{ term }, m_definition{ definition }{}

        // TODO: Code the missing prototype functions and operators
        //       that the class needs in order to work with the Queue class.
        //       Implement them in the Dictionary.cpp file.
        const std::string& getTermConst()const
        {
            return m_term;
        }

        Dictionary() : m_term{ "" }, m_definition{ "" }{};
        std::ostream& display(std::ostream& ostr = std::cout);

       
    };
    bool operator==(const Dictionary& lhs, const Dictionary& rhs);

    std::ostream& operator<<(std::ostream& ostr, Dictionary& D);
}
#endif
EN

回答 1

Stack Overflow用户

发布于 2022-09-30 23:11:00

这个问题是由operator <<dictionary.h中的定义引起的。

代码语言:javascript
代码运行次数:0
运行
复制
std::ostream& operator<<(std::ostream& ostr, Dictionary& D);

operator <<要求其操作数不会更改。如果通过引用传递参数,请确保添加const关键字。

代码语言:javascript
代码运行次数:0
运行
复制
std::ostream& operator<<(std::ostream& ostr, const Dictionary& D);

或者只是通过值传递参数,比如

代码语言:javascript
代码运行次数:0
运行
复制
std::ostream& operator<<(std::ostream& ostr, Dictionary D);

下面是我测试的代码:

main.cpp (用new和局部变量测试,两者都有效)

代码语言:javascript
代码运行次数:0
运行
复制
#include <iostream>
#include <iomanip>
#include "queue.h"
#include "dictionary.h"

int cout = 0; // won't compile if headers don't follow convention

int main(int argc, char** argv)
{
    std::cout << "Command Line:\n";
    std::cout << "--------------------------\n";
    for (int i = 0; i < argc; i++)
        std::cout << std::setw(3) << i + 1 << ": " << argv[i] << '\n';
    std::cout << "--------------------------\n\n";

    // Data to be used by the template classes
    long   dataSetA[]{ 22, 1, 455, 90, 455, 6, 43 };
    double dataSetB[]{ 34.556, 3333.3303, 122.55, 3333.3333, 678.54, 3333.3389 };
    sdds::Dictionary pData[]{
        sdds::Dictionary("dome", "a rounded vault forming the roof of a building or structure."),
        sdds::Dictionary("pollution", "An introduction into the environment of a substance that has harmful effect"),
        sdds::Dictionary("slide", "a structure with a smooth sloping surface for children to slide down."),
        sdds::Dictionary("complete", "having all the necessary or appropriate parts"),
        sdds::Dictionary("pollution", "An introduction into the environment of a substance that has harmful effect"),
        sdds::Dictionary("slide", "a structure with a smooth sloping surface for children to slide down."),
        sdds::Dictionary("complete", "having all the necessary or appropriate parts"),
        sdds::Dictionary("acceptable", "able to be agreed on; suitable"),
        sdds::Dictionary("dome", "a rounded vault forming the roof of a building or structure.")
    };

    {
        std::cout << "******************************************************\n";
        std::cout << "* Testing the UniqueQueue of Pairs                   *\n";
        std::cout << "******************************************************\n";
        sdds::Queue<sdds::Dictionary, 100>* UniqueQueueDictionary = new sdds::Queue<sdds::Dictionary, 100>();

        for (const auto& item : pData)
            UniqueQueueDictionary->push(item);

        std::cout << "UniqueQueueue size: [" << UniqueQueueDictionary->size() << "]\n";
        UniqueQueueDictionary->display();

        std::cout << "Item [1]: [" << (*UniqueQueueDictionary)[1].getTerm() << "]["
        << (*UniqueQueueDictionary)[1].getDefinition() << "]\n";
        std::cout << "Item [4]: [" << (*UniqueQueueDictionary)[4].getTerm() << "]["
        << (*UniqueQueueDictionary)[4].getDefinition() << "]\n";
        std::cout << "Item [11]: [" << (*UniqueQueueDictionary)[11].getTerm() << "]["
        << (*UniqueQueueDictionary)[11].getDefinition() << "]\n";

        std::cout << "----------------------\n";
        std::cout << "| Testing Pop        |\n";
        std::cout << "----------------------\n\n";

        std::cout << "Popped Item: " << UniqueQueueDictionary->pop() << std::endl;// --> Error line
        std::cout << "----------------------\n";
        UniqueQueueDictionary->display();


        sdds::Queue<sdds::Dictionary, 100> qd;

        for (const auto& item : pData)
            qd.push(item);

        std::cout << "UniqueQueueue size: [" << qd.size() << "]\n";
        qd.display();

        std::cout << "Item [1]: [" << qd[1].getTerm() << "]["
        << qd[1].getDefinition() << "]\n";
        std::cout << "Item [4]: [" << qd[4].getTerm() << "]["
        << qd[4].getDefinition() << "]\n";
        std::cout << "Item [11]: [" << qd[11].getTerm() << "]["
        << qd[11].getDefinition() << "]\n";

        std::cout << "----------------------\n";
        std::cout << "| Testing Pop        |\n";
        std::cout << "----------------------\n\n";

        std::cout << "Popped Item: " << qd.pop() << std::endl;// --> Error line
        std::cout << "----------------------\n";
        qd.display();


        std::cout << "******************************************************\n\n";
        delete UniqueQueueDictionary;
    }

    return cout;
}

queue.h

代码语言:javascript
代码运行次数:0
运行
复制
#ifndef QUEUE_H_
#define QUEUE_H_
#include "dictionary.h"

namespace sdds
{
    template <typename T, unsigned int CAPACITY>
    class Queue
    {
/*        // the following 3 lines wont compile with gcc-4.8.5 or ealier, but copile with gcc-7.1.0
        T m_item[CAPACITY]{};
        unsigned int m_size{};
        T dummy{};
*/
        // the following 3 lines will compile with gcc-4.8.5 or gcc-4.4.7
        T m_item[CAPACITY];
        unsigned int m_size;
        T dummy;

    public:
        Queue()
        {
            m_size = 0; // need to initialize for older gcc compiler
        };
        T pop()
        {
            if (m_size > 0)
            {
                T temp = m_item[0];
                for (size_t i = 0; i < m_size - 1; i++)
                {
                    m_item[i] = m_item[i + 1];
                }
                m_size--;
                return temp;
            }
            return dummy;
        }
        size_t size()
        {
            return m_size;
        };
        std::ostream& display(std::ostream& ostr = std::cout)
        {
            ostr << "----------------------" << std::endl;
            ostr << "| Dictionary Content |" << std::endl;
            ostr << "----------------------" << std::endl;
            for (unsigned int i = 0; i < m_size; i++) {
                ostr << m_item[i] << std::endl;
            }
            ostr << "----------------------" << std::endl;
            return ostr;
        }
        virtual bool push(const T& item)
        {
            bool suc = false;
            if (m_size < CAPACITY)
            {
                m_item[m_size] = item;
                m_size++;
                suc = true;
            }
            return suc;
        };

    

       virtual T& operator[](size_t index)
       {
           if (index >= 0 && index < CAPACITY)
           {
               return m_item[index];
           }
           return dummy;
       }
       virtual ~Queue()
       {
       }
    };
    template <>
    Queue<Dictionary, 100u>::Queue()
    {
        Dictionary a;
        dummy = a;
        m_size = 0;      // need to initialize for older gcc compiler
    };

}
#endif

dictionary.h

代码语言:javascript
代码运行次数:0
运行
复制
#ifndef DICTIONARY_H_
#define DICTIONARY_H_

#include <string>
#include <iostream>
namespace sdds
{
    class Dictionary
    {
        std::string m_term{};
        std::string m_definition{};
    public:
        const std::string& getTerm() const
        {
            return m_term;
        }
        const std::string& getDefinition() const
        {
            return m_definition;
        }
        Dictionary(const std::string& term, const std::string& definition) : m_term{ term }, m_definition{ definition }{}

        // TODO: Code the missing prototype functions and operators
        //       that the class needs in order to work with the Queue class.
        //       Implement them in the Dictionary.cpp file.
        const std::string& getTermConst() const
        {
            return m_term;
        }

        Dictionary() : m_term{ "" }, m_definition{ "" }{};
        std::ostream& display(std::ostream& ostr = std::cout);
   
        Dictionary& operator=(const Dictionary& d);
        bool operator==(const Dictionary& d);
    };
    bool operator==(const Dictionary& lhs, const Dictionary& rhs);
    std::ostream& operator<<(std::ostream& ostr, Dictionary D);
//    std::ostream& operator<<(std::ostream& ostr, const 
Dictionary& D);
}

#endif

dictionary.cpp

代码语言:javascript
代码运行次数:0
运行
复制
#include "dictionary.h"
#include <iostream>

std::ostream& sdds::Dictionary::display(std::ostream& ostr) {

   return ostr << *this;
}

// Assignment operator
sdds::Dictionary& sdds::Dictionary::operator=(const 
sdds::Dictionary& r) {
    this->m_term = r.getTerm();
    this->m_definition = r.getDefinition();
    return *this;
}

// Comparison operator
bool sdds::Dictionary::operator==(const sdds::Dictionary& d) {
    return m_term.compare(d.getTerm())==0 && m_definition.compare(d.getDefinition())==0;
}

// Comparison operator - outside the class, but in the namespace
bool sdds::operator==(const sdds::Dictionary& l, const sdds::Dictionary& r) {
    return l.getTerm().compare(r.getTerm())==0 && l.getDefinition().compare(r.getDefinition())==0;
}

/** define the << operator inside the namespace - works (passes D as value will not change it in the caller) */
std::ostream& sdds::operator<<(std::ostream&ostr, sdds::Dictionary D) {
    ostr << "[" << D.getTerm() << "][" << D.getDefinition() << "]";
    return ostr;
}

/** define the << operator inside the namespace - works (passes D as reference, make sure to add const.)
std::ostream& sdds::operator<<(std::ostream&ostr, const sdds::Dictionary& D) {
    ostr << "[" << D.getTerm() << "][" << D.getDefinition() << "]";
    return ostr;
}
*/

/** define the << operator out of the namespace - works
std::ostream&operator<<(std::ostream&ostr, const sdds::Dictionary& D) {
    ostr << "[" << D.getTerm() << "][" << D.getDefinition() << "]";
    return ostr;
}
*/

用gcc-4.8.5及更高版本编写的代码,而gcc-4.4.7编写的代码失败

代码语言:javascript
代码运行次数:0
运行
复制
g++ -std=c++0x -o main.e main.cpp dictionary.cpp

输出

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73903359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档