【发布时间】: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<string, LIFO, maxElements> lfo = input;