Auto not being recognised by the compiler, what would be the best replacement? [closed]

StackOverflow https://stackoverflow.com/questions/19441277

  •  01-07-2022
  •  | 
  •  

Domanda

So I have wrote a program that uses auto however the compiler doesn't seem to recognize it, probably it is an earlier compiler.

I was wondering for my code, what are suitable variables to fix my code so that I do not need to use the auto keyword? I'm thinking a pointer to a string? or a string iterator, though I am not sure.

  #include <cstdlib>
    #include <string>
    #include <iostream>
    #include <unistd.h>
    #include <algorithm>
    using namespace std;

    int main(int argc, char* argv[]) {

        enum MODE {
            WHOLE, PREFIX, SUFFIX, ANYWHERE, EMBEDDED
        } mode = WHOLE;
        bool reverse_match = false;

        int c;
        while ((c = getopt(argc, argv, ":wpsaev")) != -1) {
            switch (c) {
                case 'w': // pattern matches whole word
                    mode = WHOLE;
                    break;
                case 'p': // pattern matches prefix
                    mode = PREFIX;
                    break;
                case 'a': // pattern matches anywhere
                    mode = ANYWHERE;
                    break;
                case 's': // pattern matches suffix
                    mode = SUFFIX;
                    break;
                case 'e': // pattern matches anywhere
                    mode = EMBEDDED;
                    break;
                case 'v': // reverse sense of match
                    reverse_match = true;
                    break;
            }
        }
        argc -= optind;
        argv += optind;

        string pattern = argv[0];


        string word;
        int matches = 0;

        while (cin >> word) {

            switch (mode) {
                case WHOLE:
                    if (reverse_match) {
                        if (pattern != word) {
                            matches += 1;
                            cout << word << endl;
                        }
                    } else if (pattern == word) {
                        matches += 1;
                        cout << word << endl;
                    }
                    break;
                case PREFIX:
                    if (pattern.size() <= word.size()) {
                        auto res = mismatch(pattern.begin(), pattern.end(), word.begin());

                        if (reverse_match) {
                            if (res.first != word.end()) {
                                matches += 1;
                                cout << word << endl;
                            }
                        } else if (res.first == word.end()) {
                            matches += 1;
                            cout << word << endl;
                        }
                    }
                    break;
                case ANYWHERE:

                    if (reverse_match) {
                        if (!word.find(pattern) != string::npos) {
                            matches += 1;
                            cout << word << endl;
                        }
                    } else if (word.find(pattern) != string::npos) {
                        matches += 1;
                        cout << word << endl;
                    }
                    break;
                case SUFFIX:
                    if (pattern.size() <= word.size()) {
                        auto res = mismatch(pattern.rbegin(), pattern.rend(), word.rbegin());

                        if (reverse_match) {
                            if (res.first != word.rend()) {
                                matches = +1;
                                cout << word << endl;
                            }
                        } else if (res.first == word.rend()) {
                            matches = +1;
                            cout << word << endl;
                        }
                    }
                    break;
                case EMBEDDED:
                    if (reverse_match) {
                        if (!pattern.find(word) != string::npos) {
                            matches += 1;
                            cout << word << endl;}

                    } else if (pattern.find(word) != string::npos) {
                        matches += 1;
                        cout 

<< word << endl;
                }
                break;
        }

    }
    return (matches == 0) ? 1 : 0;
}

Thanks in advance!

Errors I get:

main.cpp:70:26: error: 'res' does not name a type
main.cpp:73:29: error: 'res' was not declared in this scope
main.cpp:77:32: error: 'res' was not declared in this scope
main.cpp:97:26: error: 'res' does not name a type 
main.cpp:100:29: error: 'res' was not declared in this scope 
main.cpp:104:32: error: 'res' was not declared in this scope
È stato utile?

Soluzione

Since you are using auto to declare variables saving the return value of mismatch, it could be replaced with the return type of said function. According to this, this would be

std::pair<InputIt1,InputIt2>.

In your first use this would mean InputIt1=InputIt2=std::string::iterator(the return type of std::string::begin) and the result type should be std::pair

In your second it would be the return type of std::string::rbegin():

std::pair<std::string::reverse_iterator,std::string::reverse_iterator>

I hope this helps, but it is of course rather tedious to enter such long typenames and auto should, imho, be prefered, so if you can use a more modern compiler(or pass the right flags to your current one(-std=c++11 for g++ for instance)), i suggest you do so.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top