gpt4 book ai didi

c++ - 这是一个 VS2008 错误吗?功能样式变量初始化

转载 作者:可可西里 更新时间:2023-11-01 17:56:59 27 4
gpt4 key购买 nike

在下面的代码中,行

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

结果为 C2061 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.

如果我改成

const char * const * eNames = names+cntNames; 

它没有提示。这是编译器错误吗?如果不是,为什么要投诉?

我的“关于”框显示:版本 9.0.30729.1 SP

我的 GCC 同事没有看到这个错误。

#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;
}

最佳答案

这绝对是一个编译器错误。证人:

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

关于c++ - 这是一个 VS2008 错误吗?功能样式变量初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16463743/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com