如何在Visual c++中使用c++ 11的特性,比如委托构造函数
How can I use C++11 features like delegating constructors in Visual C++ November 2012 CTP?
我安装了visual c++ 2012年11月CTP,但似乎我做错了,因为我仍然不能使用委托构造函数
-
我将平台工具集设置为:Microsoft Visual c++ Compiler Nov2012 CTP (v120_CTP_Nov2012)
-
我的代码:
#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:任何关于c++11和visual studio 2012的提示都非常受欢迎。谢谢。
LE:这是实现文件:
#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;
}
在无参数构造函数上得到以下错误消息:"Hero"不是"Hero"类的非静态数据成员或基类
你引用的错误信息是由IntelliSense报告的,它还不支持新的c++ 11语言特性。请注意,错误消息的全文如下(强调我的):
智能感知: "Hero"不是类"Hero"的非静态数据成员或基类
十一月CTP的公告声明(强调我的):
虽然提供了一个新的平台工具集,以便将编译器集成为Visual Studio 2012构建环境的一部分,但 VS 2012 IDE,智能感知,调试器,静态分析和其他工具基本保持不变,并且尚未提供对这些新的c++ 11功能的支持。
编译器,其中是由11月CTP更新的,拒绝代码并出现以下错误:
error C2511: 'void Hero::SetName(const std::string &)' : overloaded member function not found in 'Hero'
c:jmscratchtest.cpp(6) : see declaration of 'Hero'
error C2511: 'void Hero::SetLevel(const int &)' : overloaded member function not found in 'Hero'
c:jmscratchtest.cpp(6) : see declaration of 'Hero'
这些错误是预料之中的,因为你的代码是病态的(SetLevel
和SetName
的参数在它们的内联声明中是通过值传递的,在它们的定义中是通过引用传递的)。当这些错误被修复后,编译器接受你的代码。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 当从函数参数中的临时值调用复制构造函数时
- 在c++构造函数中使用随机字符串生成器
- 一对向量构造函数:初始值设定项列表与显式构造
- 从构造函数抛出异常时如何克服内存泄漏
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 具有默认模板类型的默认构造函数的类型推导
- 使用dynamic_cast和构造函数时出错
- 在c++中使用向量时,如何调用构造函数和析构函数
- 奇怪的构造函数行为