【问题标题】:C++ 11 Delegate Constructor With New Instance Parameter?具有新实例参数的 C++ 11 委托构造函数?
【发布时间】:2012-12-31 08:40:12
【问题描述】:

在使用 Visual Studio Nov 2012 CTP C++ Compiler 编译此语法时遇到问题……只是想确保我没有遗漏一些明显的东西。

谢谢!

编辑:删除了 Header 以使其更加简单。

class Location
{
public:
    Location();
};

class Shape
{
public:
    Shape();
    Shape(Location location);
};


// Doing this by pointer works ...
// Shape::Shape(Location* location){}
// Shape::Shape() : Shape(new Location()){}

Shape::Shape(Location location)
{
}

Shape::Shape()
    : Shape(Location())
    // error C2143: syntax error: missing ';' before ':'
{
    // int x = 0;
    // (void) x;  // Added these two lines in some cases to get it to compile.
    // These two lines do nothing, but get around a compiler issue.
}

【问题讨论】:

  • 如果这是编译器的内部错误,则意味着编译器出现问题,而不是您的代码。在我看来这是正确的。您可能想在 MSDN 上提交错误报告。
  • 应该永远发生内部错误。如果是这样,它总是编译器的一个错误,而不是你的错。
  • 作为旁注/讨厌,使用void 表示函数不带参数... AUGH
  • 你也忘记了所有的分号。也许你的编译器只是生你的气,想让你别管它。
  • 我猜这不是真正的代码,缺少分号,没有前向声明等。

标签: c++ c++11 constructor visual-studio-2012 delegation


【解决方案1】:
// .h Simplification
class Location
{
public:
  Location() {}
  Location(Location const& other) {}
};

class Shape
{
  Shape();
  Shape(Location location);
};

// How about by value or reference?
Shape::Shape(Location location)
{
}

Shape::Shape(void)
  : Shape(Location()) // error C1001: An internal error has occurred in the compiler.
{
}

int main() {}

以上代码在gcc 4.7.2中编译运行

我必须对您的代码进行一些更改才能使其编译。在简化事情时,尽量保持简化的代码编译。 http://sscce.org/

【讨论】:

  • 多个副本是怎么回事?
  • @Yakk 我的代码和你的代码都出现了同样的错误。由于它适用于 GCC,而且现在我刚刚被告知最新版本的编译器,post CTP 可以干净地编译它,我将其归结为 CTP 编译器中的一个错误。否则,它似乎是有效的语法。我会将您的答案标记为验证它是正确的 C++ 11 语法的答案。谢谢!
  • 新增:我无法计算使用这种语法得到的不同错误的数量,但它们通常都会导致一些未知的编译器错误,或者 VS 无法与之通信的错误子进程。我设法通过添加以下内容来编译它: int x = 0; (无效) x;进入我委托给其他人的每个构造函数。不要问我为什么它有效,它只是有效,我将它作为一种解决方法,直到发布。巫毒!
猜你喜欢
  • 2016-10-02
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多