mercredi 4 mars 2020

Why can't I use this palindrome function when using an STL list?

I made a palindrome function that allows me to use any container type, and it worked for string, vector, and deque but when I made an STL list and tried running the code, I was given the following error (code below error).

Severity    Code    Description Project File    Line    Suppression State
Error   C2676   binary '-': 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator homework4   C:\...\Source.cpp   9   

Severity    Code    Description Project File    Line    Suppression State
Error   C3536   'itr1': cannot be used before it is initialized homework4   C:\...\Source.cpp   9   

Severity    Code    Description Project File    Line    Suppression State
Error   C2676   binary '<': 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator homework4   C:\...\Source.cpp   9   
Severity    Code    Description Project File    Line    Suppression State
Error   C2100   illegal indirection homework4   C:\...\Source.cpp   10  

Visual studio error list

#include <iostream>
#include<list>
#include<vector>
#include<deque>
using namespace std;

template <typename Container>
bool palindrome(const Container& s) {
    for (auto itr = s.begin(), itr1 = s.end() - 1; itr < itr1; itr++, itr1--) {
        if (*itr != *itr1)
            return false;
    }
    return true;
}

void main() {
    const string word1{ "racecar" };
    const vector<char> word2{ 'a', 'b', 'b', 'a' };
    const deque<int> word3{ 83, 84, 65, 84, 83 };
    const list<int> word4{ 83, 84, 65, 84, 83 };
    word4.end();

    cout << palindrome< string >(word1) << endl;
    cout << palindrome< vector<char> >(word2) << endl;
    cout << palindrome< deque<int> >(word3) << endl;
    cout << palindrome< list<int> >(word4) << endl;
}

Aucun commentaire:

Enregistrer un commentaire