8

In the code below, the line

const char * const * eNames (names+cntNames); 

results in a C2061 error in Visual Studio 2008:

syntax error : identifier 'identifier' - The compiler found an identifier where it wasn't expected. Make sure that identifier is declared before you use it. An initializer may be enclosed by parentheses. To avoid this problem, enclose the declarator in parentheses or make it a typedef. This error could also be caused when the compiler detects an expression as a class template argument; use typename to tell the compiler it is a type.

If I change to

const char * const * eNames = names+cntNames; 

it doesn't complain. Is this a compiler bug? If not, why the complaint?

My About box says: Version 9.0.30729.1 SP

My colleague with GCC does not see this error.

#include <string>
#include <algorithm>
#include <functional>
#include <iostream>

namespace ns1 {

   struct str_eq_to
   {
      str_eq_to(const std::string& s) : s_(s) {}
      bool operator()(const char* x) const { return s_.compare(x)==0; }
      const std::string& s_;
   };

   static bool getNameIndex(const char * const * names, size_t cntNames, const std::string& nm, int &result)
   {
      const char * const * eNames (names+cntNames);  //VS2008 error C2061: syntax error : identifier 'names'
      const char * const * p = std::find_if(names, eNames, str_eq_to(nm));
      if(p==eNames) return false;
      result = p-names;
      return true;
   }

} //namespace ns1


int main() {

   const char * const names[] = {"Apple", "Orange","Plum"};
   std::string str = "Plum";
   int res;

   ns1::getNameIndex(names, 3, str, res);
   std::cout << str << " is at index " << res << std::endl; 
   return 0;
}
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • 7
    VS 2008 is five years old, positively geriatric in compiler years, and (obviously) hasn't kept up with developments in C++ since then. I recommend you download VS 2012 Express (zero-cost, requires registration) and try it there. If it works, you can assume VS 2008 was wrong. – zwol May 09 '13 at 14:09
  • 2
    This looks suspiciously like a most-vexing-parse related problem. Try a second set of parens: `const char * const * eNames ((names+cntNames));` – Mark B May 09 '13 at 14:54
  • @MarkB - VS2008 then doesn't like double brackets and get C2059 for each bracket. – Angus Comber May 09 '13 at 15:18
  • 1
    @Zack I just tried compiling in VS 2012 Ultimate and I encountered the same error. – Otaia May 09 '13 at 16:07
  • 6
    Avoid making statements look like function prototypes and the compiler will be happy. – Hans Passant May 09 '13 at 16:23
  • Please improve the title so that future visitors to the site with the same problem can find it. – Raymond Chen May 09 '13 at 17:25

1 Answers1

4

This is most definitely a compiler bug. Witness:

extern char** a;
typedef char* cp;
char** c(a);      // error
cp* c1(a);        // no error
char** c2(c1);    // error
cp* n(0);         // no error
char** n2(0);     // error
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243