-5

I used to code in C++ 12 years ago, but left it for other simpler languages due to my job.

I'd like to renew my knowledge and tried to compile the solution proposed here, just to try this new way to iterate on vectors. But ran into a compile error:

expected initializer before ‘:’ token

I didn't know it was possible to avoid explicit declaration of iterators like that in C++ with the use of this (auto && elem : v). What version of C++ is it?

b.cpp

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <set>

int main()
{
  std::vector<std::pair<std::string, std::string>> v
  {   {"handgun", "bullets"},
      {"turret", "bullets"}};

  std::cout << "Initially: " << std::endl << std::endl;
  for (auto && elem : v)
    std::cout << elem.first << " " << elem.second << std::endl;

  return 0;
}

Compilation

$ cc b.cpp  -std=c++0x  -o myprog

Errors

b.cpp: In function ‘int main()’:
b.cpp:15: error: expected initializer before ‘:’ token
...

C++ Compilers I use: g++

$ g++ --version

gives

g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
Copyright (C) 2010 Free Software Foundation, Inc.
Community
  • 1
  • 1
J. Chomel
  • 8,193
  • 15
  • 41
  • 69

2 Answers2

9

Your compiler is too old. Ranged-based for loops weren't added until 4.6.

Also, ignore the comments. cc is usually symlinked to gcc. From the manual:

GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • The comments are not accurate, but OP still needs use g++ (or add other options). The reason for that is the next paragraph in the manual: `However, the use of gcc does not add the C++ library` – eerorika May 17 '16 at 15:40
  • 2
    @user2079303 A) The real issue is OP's compiler is too old. B) Ranged-based for loops are a language construct, they don't need `libstdc++` to work. – uh oh somebody needs a pupper May 17 '16 at 15:41
2

Following pupper's advice, I adapted my code to the below:

#include <iostream>
#include <vector>

int main()
{
    std::vector<std::pair<std::string, std::string>> v
    {   {"handgun", "bullets"},
        {"turret", "bullets"}};

    std::cout << "started: " <<  std::endl;
    std::vector<std::pair<std::string, std::string>>::iterator Current_Iterator = v.begin();
    while(Current_Iterator != v.end())
    {
        std::cout << Current_Iterator->first << " " << Current_Iterator->second << std::endl;
        Current_Iterator++;
    }
    return 0;
}

It still doesn't compile with the gcc on my system, giving

b.cpp:(.text+0x134): undefined reference to ``std::cout' ...

But it works with g++:

$ g++ b.cpp   -std=c++0x  -o myprog && ./myprog
started:
handgun bullets
turret bullets
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • I edited out the "meta" bit about your experience. Whilst I appreciate your frustration, it doesn't belong in the answer. Back yourself, you're doing fine. C++ can be dense given a host of target environments, keep at it. – Niall May 18 '16 at 07:53