如何在C ++(C ++ 0x)中使用“自动"? [英] How do i use 'auto' in C++ (C++0x)?

查看:135
本文介绍了如何在C ++(C ++ 0x)中使用“自动"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要对这段代码进行什么处理才能使其编译,这绕着这条线刹车:

What do i have to do to this code to make it compile, it's braking around this line:

自动val = what.getObject();

auto val = what.getObject();

#include<iostream>
using namespace std;

class CUP{
    public:
        void whatsHappening(){}
};

class MUG{
    public:
        void whatsHappening(){}
};

class CupThrower{
    public:
        CUP cp;
        CUP getObject(){ return cp;}
};

class MugThrower{
    public:
        MUG mg;
        MUG getObject(){return mg;}
};

template <typename T> void whatsHappening(T what){

    auto val = what.getObject(); //DOES NOT COMPILE
    val.whatsHappening();
}

int main(){
    CupThrower ct;
    MugThrower mt;
    whatsHappening(ct);
    whatsHappening(mt);
    return 0;
}

我正在使用VS2008进行编译.

i am using VS2008 to compile.

推荐答案

自动.使用VS2010和更高版本,或支持此功能的其他编译器.

Auto isn't supported in VS2008. Use VS2010 and later versions, or another compiler supporting this feature.

这篇关于如何在C ++(C ++ 0x)中使用“自动"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆