为什么不能在类定义的同一个命名空间中创建一个C ++类的实例? [英] Why Can't I create an instance of a C++ class defined in the same namespace as the class definition?

查看:194
本文介绍了为什么不能在类定义的同一个命名空间中创建一个C ++类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友正在学习C ++,我正在帮助他。我们都使用Visual studio 2010。

A friend of mine is working on learning C++ and I was helping him along. We are both using Visual studio 2010.

以下代码提供了一个错误:

the following code gives an error:

#include <iostream>
using namespace std;

namespace Characters
{
    class PlayerStats
        {
        public:
            int HPMax, HPCurrent;
        };
    PlayerStats John;
    John.HPMax = 1;
    Characters::John.HPMax = 1;
}

PlayerStats John似乎解决只是罚款,然而
(John.HPMax = 1;和Characters :: John.HPMax = 1;)
之后的行提供错误错误:此声明没有存储类或类型说明符
是非法的,以这种方式在命名空间中设置成员变量,还是有其他我缺少的?

the line "PlayerStats John;" seems to resolve just fine, however the lines after ( "John.HPMax = 1;", and "Characters::John.HPMax = 1;") give the error "Error: this declaration has no storage class or type specifier" is it illegal to set the member variables inside the namespace in this manner or is there something else that I am missing?

推荐答案

赋值语句只允许在函数中(包括构造函数或析构函数作为特殊类型的函数)。这是大多数可执行语句的真实。命名空间与此无关。这些赋值在没有它们的情况下是非法的,或者在类或结构体内部(但不在内联函数体中)。

Assignment statements are only allowed in functions (including constructors or destructors as special sorts of functions). This is true of most "executable" statements. The namespace doesn't matter for this. Those assignment would be illegal without them, or inside of a class or struct (but not in an inline function body).

只允许函数之外的声明,因此如果您需要初始化John实例的那些成员,那么您必须使用某种初始化器。

Only declarations are allowed outside of functions, so if you need those members of the John instance to be initialized then you must use some sort of initializer.

由于类(到目前为止)只有公共数据成员,没有构造函数或虚方法,你可以使用成员初始化和大括号列表:

Since the class (so far) has only public data members and no constructors or virtual methods, you could use memberwise initialization with a braced list:

PlayerStats John = {2, 1}; // sets HPMax=2 and HPCurrent=1

这种类通常被描述为struct ,并且通常只用于非常小,简单的对象。数据成员按它们声明的顺序初始化。

This sort of class is normally described as a struct instead, and is generally only used for very small, simple objects. Data members are initialized in the order they are declared.

一个更加面向对象的方法是使用构造函数。 :

A more object-oriented approach is to use a constructor. :

class PlayerStats
{
public:
    int HPMax, HPCurrent;
    PlayerStats(int max) : HPMax(max), HPCurrent(max) {}
};

// define an instance of PlayerStats, with max HP of 1:
PlayerStats John(1);

如果你有或者可以定义一个接受你需要初始化的信息的构造函数。你不能有两个不同的构造函数采用相同的数量和类型的参数,所以构造函数必须是只接受一个int作为其参数的唯一一个。

That works if you have or can define a constructor that accepts the information you need to initialize. You can't have two different constructors take the same number and types of arguments, so that constructor would have to be the only one that accepted just one int as its argument.

还有其他方法,使用工厂方法或静态类初始化,但是那些更多的涉及。以上应该让你现在去。

There are other ways, using "factory methods" or static class initialization, but those are more involved. The above should get you going for now.

这篇关于为什么不能在类定义的同一个命名空间中创建一个C ++类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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