2014-08-27 280 views
1

以下代码是一个假设代码。这是g ++(4.2.1)下的完全有效的代码。当与编译锵++(4.2)它通过改变myclass::myclass*myclass*产生误差作为qualified reference to 'myclass' is a constructor name rather than a type wherever a constructor can be declared将g ++代码移植到Clang ++问题

class myclass 
{ 
public: 
    myclass() { } 
    ~myclass() {} 

}; 

myclass::myclass* funct() { 
    return new myclass(); 
} 

我可以解决此问题。不过,我不希望更改任何代码。是否有任何我可以提供的命令行标志,以便像使用Clang ++一样编译此代码?

+3

代码不符合标准的方式是让我很惊讶,它编译于G ++ st所有。我会改变它。 – programmerjake 2014-08-27 21:54:02

+1

'myclass'是一种类型。 'myclass :: myclass'不是。 – 2014-08-27 21:55:12

+0

g ++ 4.2.1是什么,7岁? – 2014-08-27 22:12:02

回答

4

不,没有这样的标志:因为这个程序是不合格的,它不应该编译。

如果编译器编译它,那么这是一个编译器错误。 This bug report看起来像是影响你特定gcc版本的那个。

的代码应该固定:

myclass* funct() { 
    return new myclass(); 
} 
+0

或者,'class myclass :: myclass * funct()'也是合法的,尽管我看不到有人会想要这样做的任何理由。 – 2014-08-27 22:13:01

+0

@ T.C hehe是的权利。将它添加到答案只会让读者感到困惑,我相信......尽管如此,仍然可以自由编辑。 – quantdev 2014-08-27 22:14:04

+1

相关的GCC错误是[11764](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=11764)。另外,在技术上'class myclass :: myclass :: myclass :: myclass :: myclass :: myclass *'也是合法的:D – 2014-08-27 22:23:44

3

GCC 4.9.1还拒绝代码:

error: ‘myclass::myclass’ names the constructor, not the type 

除非代码库是高兴的,而旧的gcc 4.2,我看不出有任何的替代,而不是固定的代码。