5

I was trying to initialize a list of strings in c++11 using the following code, and its failing with various reasons. The error says that I need to use constructor to initialize the list, should I use something like list<string> s = new list<string> [size] ? What am I missing here?

#include<string>
#include<list>
#include<iostream>
using namespace std;

int main() {
      string s = "Mark";
      list<string> l  {"name of the guy"," is Mark"};
      cout<<s<<endl;
      int size = sizeof(l)/sizeof(l[0]);
      for (int i=0;i<size;i++) {
             cout<<l[i]<<endl;
      }
      return 0;
 }

I/O is

 strtest.cpp:8:47: error: in C++98 ‘l’ must be initialized by constructor, not 
 by ‘{...}’
 list<string> l  {"name of the guy"," is Mark"};
3
  • This isn't the problem, but do you really need the extra stuff that std::endl does? '\n' ends a line. Commented Jun 3, 2017 at 5:26
  • 2
    To get the number of elements in the list l, call l.size(). That sizeof dance only works for C-style arrays. Commented Jun 3, 2017 at 5:27
  • Your error message appears to be telling you that you are building with C++98 not 11 Commented Jun 3, 2017 at 5:28

4 Answers 4

11

You are using a compiler of c++98 instead of c++11.using this if you are using gcc

g++ -std=c++11 -o strtest strtest.cpp

you can replace c++11 with gnu++11

1
  • Thanks, I hardly realized that I was not using c++11, I guess I was scared at the amount of errors I saw and didn't catch this one Commented Jun 3, 2017 at 5:34
10

List initializers are only available in C++11. To use C++11 you probably have to pass a flag to the compiler. For GCC and Clang this is -std=c++11.

Also, std::list does not provide a subscript operator. You could either use a std::vector as in the other answer or you use a range-based for loop to iterate over the list.

Some more hints:

#include <string>
#include <list>
#include <iostream>

int main() {
  std::string s = "Mark";
  std::list<std::string> l {"name of the guy"," is Mark"};

  for (auto const& n : l)
    std::cout << n << '\n';
}
2
  • why should you never use using namespace std?
    – Makogan
    Commented Jun 3, 2017 at 5:36
  • 1
    @Makogan See the link. Commented Jun 3, 2017 at 5:37
0

The biggest problem here is that you are using lists. In C++ lists are doubly linked lists, as such the [] doesn't make any sense. You should be using vectors instead.

I'd try:

#include<string>
#include<vector>
#include<iostream>
using namespace std;

int main() {
      string s = "Mark";
      vector<string> l = {"name of the guy"," is Mark"};
      cout<<s<<endl;
      for (int i=0;i<l.size();i++) {
             cout<<l[i]<<endl;
      }
      return 0;
 }

instead

EDIT: as others pointed out, make sure you are compiling with c++ 11 and not c++ 98

4
  • Thanks!, what would be use case for using lists then? Can you please explain only if its not too complex? Commented Jun 3, 2017 at 5:35
  • Well do you know what a linked list is?
    – Makogan
    Commented Jun 3, 2017 at 5:35
  • I do, its just that the term "list" is so straightforward and compelling for me to use it in a "regular" context. I guess I was asking why would some one name a doubly linked list as just "list"? Commented Jun 3, 2017 at 5:37
  • @AparnaChaganti The answers to the following question should help: vector vs. list in STL Commented Jun 3, 2017 at 5:43
0

Well the answer to this problem is to just simply copy the contents of one list into another list hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.