简体   繁体   English

C ++错误-运算符不匹配=

[英]C++ error — No match for operator =

class RLE_v1
{
    struct header
    {
        char sig[4];
        int fileSize;
        unsigned char fileNameLength;
        std::string fileName;
    } m_Header;

    RLE<char> m_Data;

public:
    void CreateArchive(const std::string& source)
    {
        std::ifstream::pos_type size;
        char* memblock;
        std::ifstream file (source, std::ios::in|std::ios::binary|std::ios::ate);
        if (file.is_open())
        {
            size = file.tellg();
            memblock = new char [static_cast<unsigned int>(size)];
            file.seekg (0, std::ios::beg);
            file.read (memblock, size);
            file.close();
            //
            // trying to make assignment to m_Data here.
            //
            delete[] memblock;
        }
    }

    void ExtractArchive(const std::string& source);
};

I'm trying to copy the data in the "memblock" char array into the variable m_Data, but when I try to do it I get the error that no match for operator "=" matches these operands . 我正在尝试将“ memblock” char数组中的数据复制到变量m_Data中,但是当我尝试执行此操作时,我得到一个错误,即no match for operator "=" matches these operands I have no idea how to make them equal, because m_Data is already of type char . 我不知道如何使它们相等,因为m_Data已经是char类型。

This is the RLE class that has the variable m_Data as a mem 这是RLE类,具有变量m_Data作为内存

template <typename T>
struct RLE
{

    T* m_Data;  // Memory which stores either compressed or decompressed data
    int m_Size; // Number of elements of type T that data is pointing to

    RLE()
        : m_Data(nullptr)
        , m_Size(0)
    { }

    ~RLE()
    {
        delete m_Data;
    }
};

You've shown everything except the actual line that produces the error. 除了产生错误的实际行,您已经显示了所有内容。

But what I see is this. 但是我看到的是这个。 You have a class that has the member: 您有一个具有成员的类:

RLE<char> m_Data;

After template expansion, that struct will have the member: 模板扩展后,该结构将具有成员:

char *m_Data;

You say there is no operator= when you assign memblock to m_Data . 你说有没有operator=当你将memblockm_Data So I can only conclude that you are doing this: 所以我只能得出结论,您正在这样做:

m_Data = memblock;

Where you should actually be doing this: 实际应该在哪里执行此操作:

m_Data.m_Data = memblock;
m_Data.m_Size = size;

Instead of directly operating on a struct's internals, you might be better off making a function: 与其直接在结构的内部操作,不如创建函数更好:

template <typename T>
void RLE<T>::Alloc( size_t size )
{
    if( m_Data != nullptr ) delete [] m_Data;
    m_Data = new T [size];
    m_Size = size;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM