1

I am fairly new to C++ so this may be an easy one. 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.

user2921678
  • 11
  • 1
  • 2

1 Answers1

-1

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.

e.g. 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. If you wanted it to contain something, then you could specify it in within the curly brackets, e.g.

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

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 (i.e. -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). 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.

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.

alrikai
  • 4,123
  • 3
  • 24
  • 23
  • This is just the beginning of my program so that may explain why it doesn't look like a stack. This answer was very educational and I definitely understand namespaces more, however I am still receiving errors and am unable to compile. Where the array 'empty {}' is declared in the initialization list my compiler is expecting a '(' and between the other 2 brackets after where the body would go it is expecting a declaration. – user2921678 Oct 26 '13 at 01:27
  • What compiler are you using? – alrikai Oct 26 '13 at 01:31
  • Microsoft Visual Studio 2012 C++ Compiler – user2921678 Oct 26 '13 at 01:36
  • The first release of MSVC 2012 didn't support uniform initializers, (although apparently the CTP does), so that might be your problem. It works fine with GCC and Clang. If you can't use uniform initializers, then you can't initialize a const member array in an initializer list. – alrikai Oct 26 '13 at 01:42
  • You might want to just switch your `empty` array to be a vector. See this other SO question http://stackoverflow.com/questions/161790/initialize-a-const-array-in-a-class-initializer-in-c – alrikai Oct 26 '13 at 01:45