所有,最近我尝试使用c ++ 11支持的新功能,但我编写了这样的语句,但编译器运行失败.
auto x = 1;
Run Code Online (Sandbox Code Playgroud)
下面列出的报告错误:
D:\DEV\CBCppTest\main.cpp||In function 'int main()':|
D:\DEV\CBCppTest\main.cpp|22|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
D:\DEV\CBCppTest\main.cpp|22|error: 'x' does not name a type|
||=== Build finished: 1 errors, 1 warnings ===|
Run Code Online (Sandbox Code Playgroud)
为什么MinGW上的最后一个gcc版本4.7.0不支持这个语句.但vs10的编译器通过了.有人能知道这个问题的原因吗?
我很困惑在C++中返回const引用.所以我在下面编写代码块并在gnu c ++和visual studio上进行测试.并找到不同的答案.任何人都可以使用C++中的返回const引用来说明好处,并且原因会导致不同编译器的行为不同.
#include <iostream>
using namespace std;
class A
{
public:
A(int num1, int num2):m_num1(num1), m_num2(num2)
{
cout<<"A::A"<<endl;
}
const A& operator * (const A & rhs) const
{
return A(this->m_num1 * rhs.m_num1, this->m_num2*rhs.m_num1);
}
A(const A& rhs)
{
this->m_num1 = rhs.m_num1;
this->m_num2 = rhs.m_num2;
cout<<"A::A(A&)"<<endl;
}
const A& operator = (const A& rhs)
{
cout<<"A::Operator="<<endl;
return *this;
}
void Display();
private:
int m_num1;
int m_num2;
};
void A::Display()
{
cout<<"num1:"<<m_num1<<" num2:"<<m_num2<<endl;
}
int main()
{
A a1(2,3), …Run Code Online (Sandbox Code Playgroud) c++ ×2