【问题标题】:Issue with converting template specializations in C++在 C++ 中转换模板特化的问题
【发布时间】:2020-03-16 14:33:55
【问题描述】:

我想使用 =() 运算符将我的 LIFO 模板特化转换为 FIFO 模板特化。我试图找出问题可能是什么,但我不断收到相同的错误。

在 LFO.h 中,我有 2 种 LFO 模板特化,一种用于 LIFO,一种用于 FIFO。目标是将 LIFO 转换为 FIFO 并反转。我想这样做最终将 2 个对象与 ==() 运算符进行比较。我在转换时遇到错误,在比较时遇到错误。

我的代码:

LFO.h

#pragma once
#include <vector>
#include <iostream>
#include <string>

using namespace std;

enum enumBehavior { LIFO, FIFO };

template<typename DateType, enumBehavior Behavior, int MaxElements> class LFO {

};

template<int MaxElements> class LFO<string, LIFO, MaxElements> {
protected:
    vector<string> vector;
public:
    void Push(string elem) {
        if ((int)vector.size() < MaxElements) {
            vector.push_back(elem);
        }
        else
            cout << "LIFO full - couldn't push element\n";
    }
    bool Pop() {
        vector.clear();
    }
    const string Top() const {
        string returnString = "";
        for (int i = (int)vector.size() - 1; i >= 0; i--) {
            returnString += vector[i];
        }
        return returnString;
    }
    bool isEmpty() const {
        return (int)vector.size() <= 0 ? true : false;
    }
    bool isFull() const {
        return (int)vector.size() >= MaxElements ? true : false;
    }
    bool operator==(const LFO<string, LIFO, MaxElements>& listToAppend) {
        LFO list(listToAppend);
        return this->Top() == list.Top();
    }
    LFO<string, LIFO, MaxElements>& operator=(const LFO<string, FIFO, MaxElements>& other) {
        LFO<string, FIFO, MaxElements> list(other);
        this->Pop();
        this->Push(list.Top());
        return *this;
    }
};

template<int MaxElements> class LFO<string, FIFO, MaxElements> {
protected:
    vector<string> vector;
public:
    void Push(string elem) {
        if ((int)vector.size() < MaxElements) {
            vector.push_back(elem);
        }
        else
            cout << "FIFO full - couldn't push element\n";
    }
    bool Pop() {
        vector.clear();
    }
    const string Top() const {
        string returnString = "";
        for (int i = 0; i < (int)vector.size(); i++) {
            returnString += vector[i];
        }
        return returnString;
    }
    bool isEmpty() const {
        return (int)vector.size() <= 0 ? true : false;
    }
    bool isFull() const {
        return (int)vector.size() >= MaxElements ? true : false;
    }
    bool operator==(const LFO<string, FIFO, MaxElements>& listToAppend) {
        LFO list(listToAppend);
        return this->Top() == list.Top();
    }
    LFO<string, FIFO, MaxElements>& operator=(const LFO<string, LIFO, MaxElements>& other) {
        LFO<string, LIFO, MaxElements> list(other);
        this->Pop();
        this->Push(list.Top());
        return *this;
    }
};

StringLifo.h

#pragma once
#include "LFO.h"

template<int maxElements> class StringLIFO:
    public LFO<string, LIFO, maxElements>
{
public:
    bool Palindrome(const LFO<string, FIFO, maxElements>& input) {
        LFO<string, LIFO, maxElements>& lfo = input;                   //Error 1
        bool equal = this == lfo;                                      //Error 2
        return equal;
    }
};

我不断收到此错误:

Error   C2440   'initializing': cannot convert from 'const LFO<std::string,FIFO,30>' to 'LFO<std::string,LIFO,30> &'    Palindrome  C:\Users\J\source\repos\Palindrome\Palindrome\StringLIFO.h  16  

Error   C2679   binary '==': no operator found which takes a right-hand operand of type 'LFO<std::string,LIFO,30>' (or there is no acceptable conversion)   Palindrome  C:\Users\J\source\repos\Palindrome\Palindrome\StringLIFO.h  17  

我想知道我的代码有什么问题?


编辑

通过在StringLIFO.h 中更新函数Palindrome(),我仍然遇到一个错误。

bool Palindrome(const LFO<string, FIFO, maxElements>& input) {
        const LFO<string, LIFO, maxElements>& lfo = input;
        return (*this == lfo);
    }

现在的错误是:

binary '=': no operator found which takes a left-hand operand of type 'const LFO<std::string,LIFO,30>' (or there is no acceptable conversion)

也许输出会有用:

1>C:\Users\J\source\repos\Palindrome\Palindrome\StringLIFO.h(16,1): error C2440: 'initializing': cannot convert from 'const LFO<std::string,FIFO,30>' to 'const LFO<std::string,LIFO,30> &'
1>C:\Users\J\source\repos\Palindrome\Palindrome\StringLIFO.h(16,1): message : Reason: cannot convert from 'const LFO<std::string,FIFO,30>' to 'const LFO<std::string,LIFO,30>'
1>C:\Users\J\source\repos\Palindrome\Palindrome\StringLIFO.h(16,8): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>C:\Users\J\source\repos\Palindrome\Palindrome\StringLIFO.h(15): message : while compiling class template member function 'bool StringLIFO<30>::Palindrome(const LFO<std::string,FIFO,30> &)'
1>C:\Users\J\source\repos\Palindrome\Palindrome\Palindrome.cpp(845): message : see reference to function template instantiation 'bool StringLIFO<30>::Palindrome(const LFO<std::string,FIFO,30> &)' being compiled
1>C:\Users\J\source\repos\Palindrome\Palindrome\Palindrome.cpp(801): message : see reference to class template instantiation 'StringLIFO<30>' being compiled

【问题讨论】:

  • 你们班提供operator==吗?
  • 在第一个错误中,您尝试从 const 引用创建非 const 引用。我猜你只是想要LFO&lt;string, LIFO, maxElements&gt; lfo = input;

标签: c++ templates


【解决方案1】:

第一个错误是因为缺少const:您尝试将 const 引用分配给非 const 引用

const LFO<string, LIFO, maxElements>& lfo = input; 
^~~~~

其次是因为您试图将指针 this 与对 LFO 对象的引用进行比较。您似乎想取消引用this。我建议在涉及多个操作数时使用括号,即使没有它们也可以:

bool equal = (*this == lfo); 
              ^

短版:

bool Palindrome(const LFO<string, FIFO, maxElements>& input) {
    return *this == input;
}

【讨论】:

  • 感谢您的回答!第一个错误解决了!我仍然有这个错误:cannot convert from 'const LFO&lt;std::string,FIFO,30&gt;' to 'const LFO&lt;std::string,LIFO,30&gt; &amp;' .
  • 我想知道为什么会出现这个错误。我在=() 运算符中传递了一个常量引用。
  • @Jordy 这很奇怪。 const LFO&lt;std::string,FIFO,30&gt;const LFO&lt;std::string,FIFO,30&gt; &amp; 在您想将一个转换为另一个时没有任何真正的区别。
【解决方案2】:

我自己解决了这个问题,将函数 Palindrome() 替换为:

bool Palindrome(LFO<string, FIFO, maxElements>& input) {
    LFO<string, LIFO, maxElements> lfo;
    lfo.operator=(input);
    return this->operator==(lfo);
}

你猜怎么着??有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多