Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在构造函数调用中添加括号会导致xlc+中出现重复的参数错误+;编译程序 我们正在创建一种特定于域的语言,它生成必须在GCC和IBM XLC(AIX版本10.1版本编译器)下编译的C++代码。 一个特定的代码片段生成了C++代码,它在GCC下工作得很好,但在XLC下没有那么多。我已将代码修改到仍然触发编译错误的最小情况: //bug183.h class emptyList {}; extern emptyList weco; class otherClass { public: otherClass(); otherClass(const int & p); otherClass(const emptyList & e); int geta() {return a;} private: int a; }; class someClass { public: someClass(); someClass(const someClass & other); someClass(const otherClass & p1, const otherClass & p2); void exportState(); private: otherClass oc1; otherClass oc2; }; //bug183.cpp #include "bug183.h" #include <iostream> emptyList weco = emptyList(); otherClass::otherClass() {a = 0;} otherClass::otherClass(const int & p) {a = p;} otherClass::otherClass(const emptyList & e) {a = 1000;} someClass::someClass() {oc1 = otherClass(); oc2 = otherClass();} someClass::someClass(const someClass & other) {oc1 = other.oc1; oc2 = other.oc2;} someClass::someClass(const otherClass & p1, const otherClass & p2) {oc1 = p1; oc2 = p2;} void someClass::exportState() {std::cout << oc1.geta() << " " << oc2.geta() << std::endl;} int main() { someClass dudi; dudi.exportState(); //this line triggers the error in xlc someClass nuni = (someClass(otherClass(weco), otherClass(weco))); nuni.exportState(); return 0; }_C++_Parameters_Constructor_Parentheses_Xlc - Fatal编程技术网

在构造函数调用中添加括号会导致xlc+中出现重复的参数错误+;编译程序 我们正在创建一种特定于域的语言,它生成必须在GCC和IBM XLC(AIX版本10.1版本编译器)下编译的C++代码。 一个特定的代码片段生成了C++代码,它在GCC下工作得很好,但在XLC下没有那么多。我已将代码修改到仍然触发编译错误的最小情况: //bug183.h class emptyList {}; extern emptyList weco; class otherClass { public: otherClass(); otherClass(const int & p); otherClass(const emptyList & e); int geta() {return a;} private: int a; }; class someClass { public: someClass(); someClass(const someClass & other); someClass(const otherClass & p1, const otherClass & p2); void exportState(); private: otherClass oc1; otherClass oc2; }; //bug183.cpp #include "bug183.h" #include <iostream> emptyList weco = emptyList(); otherClass::otherClass() {a = 0;} otherClass::otherClass(const int & p) {a = p;} otherClass::otherClass(const emptyList & e) {a = 1000;} someClass::someClass() {oc1 = otherClass(); oc2 = otherClass();} someClass::someClass(const someClass & other) {oc1 = other.oc1; oc2 = other.oc2;} someClass::someClass(const otherClass & p1, const otherClass & p2) {oc1 = p1; oc2 = p2;} void someClass::exportState() {std::cout << oc1.geta() << " " << oc2.geta() << std::endl;} int main() { someClass dudi; dudi.exportState(); //this line triggers the error in xlc someClass nuni = (someClass(otherClass(weco), otherClass(weco))); nuni.exportState(); return 0; }

在构造函数调用中添加括号会导致xlc+中出现重复的参数错误+;编译程序 我们正在创建一种特定于域的语言,它生成必须在GCC和IBM XLC(AIX版本10.1版本编译器)下编译的C++代码。 一个特定的代码片段生成了C++代码,它在GCC下工作得很好,但在XLC下没有那么多。我已将代码修改到仍然触发编译错误的最小情况: //bug183.h class emptyList {}; extern emptyList weco; class otherClass { public: otherClass(); otherClass(const int & p); otherClass(const emptyList & e); int geta() {return a;} private: int a; }; class someClass { public: someClass(); someClass(const someClass & other); someClass(const otherClass & p1, const otherClass & p2); void exportState(); private: otherClass oc1; otherClass oc2; }; //bug183.cpp #include "bug183.h" #include <iostream> emptyList weco = emptyList(); otherClass::otherClass() {a = 0;} otherClass::otherClass(const int & p) {a = p;} otherClass::otherClass(const emptyList & e) {a = 1000;} someClass::someClass() {oc1 = otherClass(); oc2 = otherClass();} someClass::someClass(const someClass & other) {oc1 = other.oc1; oc2 = other.oc2;} someClass::someClass(const otherClass & p1, const otherClass & p2) {oc1 = p1; oc2 = p2;} void someClass::exportState() {std::cout << oc1.geta() << " " << oc2.geta() << std::endl;} int main() { someClass dudi; dudi.exportState(); //this line triggers the error in xlc someClass nuni = (someClass(otherClass(weco), otherClass(weco))); nuni.exportState(); return 0; },c++,parameters,constructor,parentheses,xlc,C++,Parameters,Constructor,Parentheses,Xlc,错误消失了。此外,如果我为另一个与weco一样创建的外部变量更改weco,即使我将构造函数括在括号中,错误也会消失,因此可以肯定地说,这两个条件都需要存在,才能显示此错误 有些人可能会问,为什么我们不删除括号,但是这样做可能会损害正在工作的代码的部分,所以我倾向于理解这种行为是否是C++编译器期望的,或者至少是有一个已知的解决办法。 < P>我认为 (someClass(otherClass(weco), otherClass(weco))) 正在错误地分析为第二次生成强制转换表达式的开始:

错误消失了。此外,如果我为另一个与
weco
一样创建的外部变量更改
weco
,即使我将构造函数括在括号中,错误也会消失,因此可以肯定地说,这两个条件都需要存在,才能显示此错误

有些人可能会问,为什么我们不删除括号,但是这样做可能会损害正在工作的代码的部分,所以我倾向于理解这种行为是否是C++编译器期望的,或者至少是有一个已知的解决办法。

< P>我认为

(someClass(otherClass(weco), otherClass(weco)))
正在错误地分析为第二次生成强制转换表达式的开始:

演员阵容:
一元表达式
类型id
强制转换表达式

注§8.2第2段(增加强调):

由于函数样式转换和类型id之间的相似性而产生的歧义可能会在不同的上下文中出现。模糊性表现为在函数样式转换表达式和类型声明之间的选择。解决方案是,任何在其语法上下文中可能是type-id的构造都应被视为type-id

如果您考虑CAST表达式中的类型ID的完整语法上下文,则<>代码>…(..)中的<代码>…>代码不可能;

匹配
类型id
,因为
后面的强制转换表达式不能为空。但是,如果只考虑一个令牌前向上下文,类型ID必须遵循“代码>”<代码>,那么可以应用8.2(2)。我并不真的认为标准的意图是只考虑一个先兆。 编辑 报告为gcc错误。您可能希望向xlc提交类似的错误报告

因为gcc 4.7.2似乎标记了与xlc相同的错误。我对gcc进行了一些研究,并确信问题在于gcc在发现括号内的表达式不能是type-id之前在type-id中标记错误(即,两个同名参数)

下面是一个例子:

#include <iostream>
#include <utility>

static const int zero = 0;
int main() {
  // Consistent with 8.2[2]
  // Example 1. Invalid cast.
  // Here, 'int(int(zero))' is a type-id, so the compiler must take
  // the expression to be a cast of '+3' to a function.
  std::cout << (int(int(zero))) + 3 << std::endl;
  // Example 2: No error.
  // The parenthesized expression cannot be a type-id in this context
  std::cout << (int(int(zero))) << std::endl;
  // Example 3: Syntax error: zero redefined.
  // Here the parenthesized expression could be a type-id, so it must
  // be parsed as one, even though the type-id is invalid.
  std::cout << (std::pair<int,int>(int(zero), int(zero))) + 3 << std::endl;

  // Apparently not consistent with 8.2[2]
  // Here the parenthesized expression can't be a type-id, as in example 2.
  // However, the type-id triggers a syntax error, presumably before gcc
  // figures out that it's not a cast-expression.
  std::cout << (std::pair<int,int>(int(zero), int(zero))) << std::endl;

  return 0;
}
2) 使用以下各项中的一项:

someClass nuni = (someClass(otherClass(weco), otherClass(weco_alias)));

这在GCC4.7.2和Clang3.2()上都有效。

Ha,它将其误认为是函数声明,因为这与该语法几乎相似(不用说,xlc是错误的)。尝试在
otherClass(weco)
周围添加括号,如
(someClass((otherClass(weco)),…
我不理解这些括号()为什么必须在那里。你能解释一下为什么/如何影响其他代码的某些部分吗?通过使用这些括号,你可以强制编译重新计算内部“语句”也可能强制使用复制构造函数。我确信它是合法C++,但对于人和解析器来说显然是混淆的。或者你试图完全扔掉<代码> = /COD>。<代码>
weco的声明也是如此,顺便说一句:
清空列表weco;
Pipilio,请看。@WhozCraig,这肯定会遇到最麻烦的解析。
someClass nuni(otherClass(weco),otherClass(weco))
是一个函数声明,并且是无效的。@SethCarnegie,添加更多的括号可能会避免错误的解析,但与其添加更多的混乱,不如首先删除导致问题的那些。
aa=a(B(B))
aa=(a(B(B))更好
谢谢rici,您完全正确地认识到gcc的更高版本也会导致此代码的编译错误。我们的项目使用gcc 3.4.6,但我在4.5.2版中尝试过,也遇到了一个错误。不过,我无法确定这些编译器是否按照您发布的规则进行了解析。
emptyList& weco_alias = weco;
someClass nuni = (someClass(otherClass(weco), otherClass(weco_alias)));