2
votes
#include <iostream>
#include <array>
using namespace std;

typedef int type;
typedef array <type,6> Liste;
bool appartient(type element, Liste liste);

int main()
{
    Liste maliste = {4,5,-3,12,7,-33};

    cout << appartient(13, maliste) << endl;
    cout << appartient(12, maliste) << endl;
    return 0;
}

bool appartient(type x, Liste liste) 
{
    for (auto element: liste) {  //////this line!!!!!!!
        if (x == element) return true;
    }
    return false;
}

I am working on this exercise of table in c++. Here I am writing a fonction "appartient" that verify the appartenance of an element on a list. But I have some errors in this fonction:

1/ error: expected initializer before ':' token

2/ error: expected primary-expression before 'return'

3/ error: expected ';' before 'return'

4/ error: expected primary-expression before 'return'

5/ error: expected ')' before 'return'

Why?

2
Are you compiling with support for c++11 features? What compiler and what flags are you using?François Andrieux
You seem to have some support for C++11 but not range-based for loops, so you are probably using a pretty old compiler. Which compiler and what version of it are you using?Some programmer dude
OK. Thank you ! I am trying to install a newer compiler. I also tried my code with prompt command windows and there was no problem. :)Truong Van Minh Nhon
with GCC 7.1.0 has no errorShakiba Moshiri

2 Answers

3
votes

Apparently your compiler does not support range-based for-loops, at least not with the options you are using.

Range-based for is supported since GCC v4.6, Clang v3.0, MSVC v17.0, eccp v4.5, Intel C++ v13.0, IBM XLC++ v13.1.2, Sun/Oracle C++ 5.13, Cray v8.4, PGI 2015, and HP aCC A.06.28.

Check your compiler is up to date, and check its manual on how to activate C++11 support.

-1
votes

Are you trying to use really a for loop or youu'd like to use a for_each loop?

The sintax of the "for" require that you initialize the variable that will be used as an index, and that you specify also the increments of such variable. Try this:

bool appartient(type x, Liste liste)
{
   for(auto element = 0; element <= 6; element ++)
   {
      if (x == element) return true;
   }
   return false;
}

This is just a very raw example. I am sure you might be able to make your code prettier that this. Check the sintax of the for - loop here: for loops.

I hope this could be of help.