0

I want to use c++ stl list and use iterator to print all the elements. Here is the code:

#include<list>
#include<algorithm>
#include<string>
using namespace std;
int main(int argc, char* argv[]){
        list<string> list;
        //list<double> list_double(6);
        //list<int> list_int(6, 0);
        //list<double> list_double2(6, 0,0);
        //list<int> else_list(list_int);
        //list<double> iter(list_double.begin(), list_double.end());
        list.push_front("1 jack");
        list.push_front("2 jackson");
        list.push_front("3 sally");

        list<string>::iterator itrr;
        for (itrr = list.begin(); itrr!= list.end(); itrr++){
                string temp = *itrr;
                print(temp)nt main(int argc, char* argv[]){
        list<string> list;
        //list<double> list_double(6);
        //list<int> list_int(6, 0);
        //list<double> list_double2(6, 0,0);
        //list<int> else_list(list_int);
        //list<double> iter(list_double.begin(), list_double.end());
        list.push_front("1 jack");
        list.push_front("2 jackson");
        list.push_front("3 sally");

        list<string>::iterator itrr;
        for (itrr = list.begin(); itrr!= list.end(); itrr++){
                string temp = *itrr;
                print(temp);
        }
        return 0;
}


        }
        return 0;
}

and when I tried to compile it, it shows some errors :

list.cpp:17:7: error: unexpected type name 'string': expected expression
        list<string>::iterator itrr;
             ^
list.cpp:17:16: error: cannot refer to class template 'iterator' without a template argument list
        list<string>::iterator itrr;
                    ~~^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:522:29: note: template is declared here
struct _LIBCPP_TEMPLATE_VIS iterator
                            ^
list.cpp:18:7: error: use of undeclared identifier 'itrr'
        for (itrr = list.begin(); itrr!= list.end(); itrr++){
             ^
list.cpp:18:28: error: use of undeclared identifier 'itrr'
        for (itrr = list.begin(); itrr!= list.end(); itrr++){
                                  ^
list.cpp:18:47: error: use of undeclared identifier 'itrr'
        for (itrr = list.begin(); itrr!= list.end(); itrr++){
                                                     ^
list.cpp:19:18: error: use of undeclared identifier 'itrr'
                string temp = *itrr;
                               ^

so what is wrong? Thanks!

user4581301
  • 33,082
  • 7
  • 33
  • 54
Keith
  • 53
  • 1
  • 2
  • 8
  • 3
    Stop using template names for formal ids of your variables. In conjunction with another terrible practice (`using namespace std;`) you're `list` isn't the `list` you think it is. – WhozCraig Oct 08 '18 at 23:15
  • In fact, what you must immediately do, right this instant, is [completely forget that `using namespace std;` is a part of C++](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). You've just discovered, and experienced yourself, why it is bad practice, at least for someone who is learning C++. Learn how to use namespaces correctly. Don't be afraid of a little extra typing. It doesn't take much time to type `std::` in front of every template or class that comes from the C++ library. – Sam Varshavchik Oct 08 '18 at 23:35

1 Answers1

3

Problem

list<string> list;

defines the identifier list to be a variable of type list<string>. This replaces previous definition of the identifier list as the Standard Library std::list class. That means when the compiler reaches list<string> at many later points in the code, list<string> makes no sense. <string> doesn't match anything you can do to a variable.

Solution

Take care in how you reuse identifiers. Making a list named list causes confusion for both readers and compilers, so don't do it. Give the variable a different name. It's full of names, so why not namelist?

This problem dovetails with the dangers of using namespace std;. For more on that, read Why is "using namespace std" considered bad practice?

user4581301
  • 33,082
  • 7
  • 33
  • 54