简体   繁体   English

在名称空间中定义类的构造函数

[英]Define constructor of a class inside a namespace

I am fairly new to C++ so this may be an easy one. 我对C ++相当陌生,所以这可能很简单。 I created a namespace and inside that namespace is a class. 我创建了一个名称空间,该名称空间内是一个类。 I cannot figure out how to define any of my class's constructors without receiving errors. 我无法弄清楚如何定义类的任何构造函数而不会收到错误。

#include <iostream>

namespace bill {
    const char * null ="EMPTY";
    const int MAX_STACK_SIZE = 100;
    class myStackClass {
    private:
        int i;
    public:
        myStackClass();
        myStackClass(int[], int);
        int peek();
        int pop();
        int push(int insertMe);
        const char empty[5];
    };
}
using namespace bill;
bill::myStackClass::myStackClass() //bill::myStackClass() doesn't work either
    :
    i(0)
{ //error C2448: 'i' : function-style initializer appears to be a function definition
} //"bill::myStackClass::myStackClass()" provides no initializer for:
        const member "bill::myStackClass::empty"


bill::myStackClass::myStackClass(int[],int)
{ //error C2439: 'bill::myStackClass::empty' : member could not be initialized
} //"bill::myStackClass::myStackClass(int *, int)" provides no initializer for:
        const member "bill::myStackClass::empty"

int bill::myStackClass::peek() // I am able to call methods belonging to the class
{
}

I'm sorry if any of this information is cluttered and hard to read or just downright not helpful. 很抱歉,如果其中任何信息混乱不清,难以阅读,或者完全没有帮助。 I have been reading my textbook and googling errors for hours and would really appreciate some insight. 我已经读了我的教科书和谷歌搜索错误了几个小时了,非常感谢您提供的见解。 Thank you. 谢谢。

It's a bit difficult to decipher what you're looking for, as your stack class isn't much of a stack. 由于您的堆栈类不是一个堆栈,因此很难解释您要查找的内容。 That said, just to get it to compile without warnings, you have to initialize your empty class member in the constructor initializer list, as you've declared it to be const. 就是说,为了使其能够在没有警告的情况下进行编译,您必须在构造函数初始值设定项列表中初始化empty类成员,因为您已将其声明为const。

eg if you have your constructors as: 例如,如果您的构造函数为:

bill::myStackClass::myStackClass()
  : i(0), empty {} 
{}

bill::myStackClass::myStackClass(int[],int) 
  : empty{}
{}

This will initialize your empty char array to be, well, empty. 这将初始化您的empty char数组为空。 If you wanted it to contain something, then you could specify it in within the curly brackets, eg 如果您希望它包含某些内容,则可以在大括号内指定它,例如

bill::myStackClass::myStackClass()
  : i(0), empty {'H', 'E', 'L', 'L', 'O'} 
{ 
    std::cout << empty << std::endl;                                         
}

then creating an object of your class (via the no-arg constructor) will print HELLO 然后创建您的类的对象(通过no-arg构造函数)将输出HELLO

bill::myStackClass stk; //--> prints HELLO

I should also make note that the use of uniform initializers (the curly-braces) is a C++11 feature, so depending on your compiler, you might have to supply an extra flag (ie -std=c++11) 我还应注意,统一初始化器(花括号)的使用是C ++ 11的功能,因此,取决于您的编译器,您可能必须提供一个额外的标志(即-std = c ++ 11)

You don't need to say using namespace bill if you are already pre-pending bill:: to your method definitions (which in your posted code, you are). 如果您已经在方法定义之前添加了bill:: ,则无需说using namespace bill The using namespace bill makes it such that the bill namespace is searched when resolving your identifier names, but if you're explicitly stating them to be bill within the bill namespace (via the bill:: ), then this isn't needed. using namespace bill使得在解析标识符名称时可以搜索bill命名空间,但是如果您明确声明它们是bill命名空间中的bill (通过bill:: ,则不需要这样做。

Also, the default scoping for a class if private, so it's redundant to specify the first part of a classes members to be private like that. 同样,如果是私有的,则默认为类定义范围,因此像这样将类成员的第一部分指定为私有是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM