简体   繁体   中英

How to run this C++ code?

I am learning C++ using C++ Primer 5th edition. In an exercise, I need to use auto that is in C++. The source code is given below. When I try to compile that using this:

g++ stringlength.cpp -o stringlength.exe

it does not work and doesn't correctly recognize auto . But when I compile it using:

g++ -std=c++11 stringlength.cpp -o stringlength.exe

it shows a whole list of errors I have not seen. Please tell me what should I do to successfully compile this code.

Source code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1, str2;
    cout << "Enter a sentence: " << endl;
    getline(cin, str1);
    cout << "Enter another sentence: " << endl;
    getline(cin, str2);
    auto size1 = str1.size();
    auto size2 = str2.size();
    if (size1 == str2)
    {
        cout << "The strings \"" << str1 << "\" & \"" << str2 << "\" are of same length." << endl;
    }
    else if (size1 > size2)
    {
        cout << "The string \"" << str1 << "\" is longer than \"" << str2 << "\"" << endl;
    }
    else 
    {
        cout << "The string \"" << str2 << "\" is greater than \"" << str1 << "\"" << endl;
    }
    cin.get();
    cin.get();
    return 0;
}

A fragment of the error the second time:

s.h:39,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from stringlength.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h:214:5: note: template
<class _T1, class _T2> constexpr bool std::operator==(const std::pair<_T1, _T2>&
, const std::pair<_T1, _T2>&)
     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h:214:5: note:   templa
te argument deduction/substitution failed:
stringlength.cpp:15:15: note:   mismatched types 'const std::pair<_T1, _T2>' and
 'unsigned int'
  if (size1 == str2)
               ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algoba
se.h:67:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\char_trait
s.h:39,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from stringlength.cpp:1:

First of all you need the -std=c++11 option because auto with the meaning you use has been added in C++11. So the second command line is ok. The errors you see are caused by the line

if (size1 == str2)

I guess you meant

if (size1 == size2)

When you compile with the -std=c++11 flag, you are doing so correctly, because your using the newer C++ revision that supports the auto keyword. When you compile with this flag, you are getting compile errors - which is completely normal. It's telling you that there is an issue with your code:

Your comparing a string with an unsigned int, this is evident from the line:

 mismatched types 'const std::pair<_T1, _T2>' and unsigned int'

The variable size1 has been defined implicitly, but its an unsigned int . If your beginning C++ and programming in general, I would recommend you don't use the auto keyword (which assigns things implicitly rather than explicitly) , this is so you know exactly what type you are assigning and will make life a little easier to both read and debug:

string str1, str2;

// ....

unsigned int size1 = str1.size();
unsigned int size2 = str2.size();
if (size1 == str2)
{
    // Your code here.
}

Now, you can immediately see that your attempting to compare str2 which is of type string and size1 which is of type unsigned int , which you obviously cannot do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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