How can I use C++11 features like delegating constructors in Visual C++ November 2012 CTP?

梦想与她 提交于 2019-12-13 08:33:54

问题


I installed visual c++ November 2012 CTP but it seems i do something wrong because i still can't use delegating constructors

  1. I set the Platform Toolset to : Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)

  2. This is my code:

    #pragma once
    
    #include<string>
    
    class Hero
    {
    private:
        long id;
        std::string name;
        int level;
        static long currentId;
        Hero(const Hero &hero); //disable copy constructor
        Hero& operator =(const Hero &hero); //disable assign operator
    public:
        Hero();
        Hero(std::string name, int level);
        long GetId() const { return this->id; }
        std::string GetName() const { return this->name; }
        int GetLevel() const { return this->level; }
        void SetName(std::string name);
        void SetLevel(int level);
    };
    

PS: Any tips regarding to c++11 and visual studio 2012 are more then welcomed. Thanks.

LE: This is the implementation file:

#include"Hero.h"

long Hero::currentId = 0;

Hero::Hero(std::string name, int level):name(name), level(level), id(++currentId) 
{

}

Hero::Hero():Hero("", 0)
{

}

void Hero::SetName(const std::string &name) 
{
    this->name = name; 
}

void Hero::SetLevel(const int &level) 
{
    this->level = level; 
}

I get the following error message on the parameterless constructor: "Hero" is not a nonstatic data member or base class of class "Hero"


回答1:


The error message you quote is being reported by IntelliSense, which does not yet support the new C++11 language features. Note that the full text of the error message reads (emphasis mine):

IntelliSense: "Hero" is not a nonstatic data member or base class of class "Hero"

The announcement for the November CTP states (emphasis mine):

While a new Platform Toolset is provided for convenience of integrating the compiler as part of the Visual Studio 2012 build environment, the VS 2012 IDE, Intellisense, debugger, static analysis, and other tools remain essentially unchanged and do not yet provide support for these new C++11 features.

The compiler, which is updated by the November CTP, rejects the code with the following errors:

error C2511: 'void Hero::SetName(const std::string &)' : overloaded member function not found in 'Hero'
    c:\jm\scratch\test.cpp(6) : see declaration of 'Hero'
error C2511: 'void Hero::SetLevel(const int &)' : overloaded member function not found in 'Hero'
    c:\jm\scratch\test.cpp(6) : see declaration of 'Hero'

These errors are expected because your code is ill-formed (the parameters of SetLevel and SetName are passed by value in their inline declarations, and by reference in their definitions). When these errors are fixed, the compiler accepts your code.



来源:https://stackoverflow.com/questions/14416574/how-can-i-use-c11-features-like-delegating-constructors-in-visual-c-november

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!