Yesterday I was assigned a project for my computer science class.
We're supposed to be using header files with our class definitions, but our teacher never really explained how header files work. This has lead me to a problem.
Here's what I'm looking at:
=====foo.h=====
class Foo
{
private:
const int MAX_ENTRIES;
int array[MAX_ENTRIES];
As you can tell by my posting here, this isn't working. Is there anyway to define the size of the array using a named constant, or am I going to have to use a literal constant?
Name:
Anonymous2009-03-26 17:26
EXPERT PROGRAMMING TUITION
Name:
Anonymous2009-03-26 17:28
I'm gonna help him guys.
You can't declare the class a second time in the header file. Instead, declare just the functions, like: //=====foo.cpp=====
Foo::Foo()
{
MAX_ENTRIES = 25;
}
Also, google a bit for some code samples to really get yourself familiar with header files.
>>3
For whatever reason my professor wants us to put the primitives in the header file. Unless I'm misunderstanding you, you're telling me to not bother with header files.
Name:
Anonymous2009-03-26 17:36
whatever sepples standards say, I find the practice of not declaring constants scalars in the header file PIG DISGUSTING
>>1
In the header static const int MAX_ENTRIES = 25;
And you can omit private:
In the .cpp int Foo::Foo() { } with no class definition and no assignment to MAX_ENTRIES.
Name:
Anonymous2009-03-26 17:59
Leuken-Baden Saddam Hussein Adriatic Becker freedom Peking chameleon
man colonel Uzbekistan BROMURE MD5 Operation Iraqi Freedom S Box Delta
Force quarter
Name:
Anonymous2009-03-26 17:59
>>8
I don't get what you're saying. Could you try to be a little bit more clear?
The compiler needs to know at compile time the size of the array (so it can know the size of the class, bonus points if you can figure out why).
Also, you can't set the value of a const int at runtime, so MAX_ENTRIES = 25; in the constructor won't work.
As it turns out, you can't set the values of const ints in a class declaration so the following doesn't work.
=====foo.h=====
class Foo
{
private:
const int MAX_ENTRIES = 25;
int array[MAX_ENTRIES];
public:
Foo();
};
But if the const int is static, then this works
=====foo.h=====
class Foo
{
private:
static const int MAX_ENTRIES = 25;
int array[MAX_ENTRIES];
public:
Foo();
};
If you need the array to be different sizes in different instants of the class (perhaps it will take a integer when the constructor is called), then you need to create a array in the constructor using new and delete it in the destructor using delete[].
You can also #define to accomplish this:
=====foo.h=====
#define MAX_ENTRIES 25
class Foo
{
private:
int array[MAX_ENTRIES];
public:
Foo();
};
This replaces any instants of MAX_ENTRIES with the characters 25 at runtime, with the static const int creates an unmodifiable integer in memory (containing 25), which is accessible at runtime.
Name:
Anonymous2009-03-26 19:19
Unless you go to allocating the buffer at run-time e.g. as a new'd array, or, better, a vector<>, you need to define MAX_ENTRIES ahead of the compile-time use; so completing what #12 started
=====foo.h=====
#define MAX_ENTRIES 25
class Foo
{
private:
int array[MAX_ENTRIES];
public:
Foo();
};
=====foo.cpp=====
//no 'class' in the definitions file, whatever
Foo::Foo()
{
}
There are probably compile-time ways involving templated classes and delegation, but that's probably too complicated for the homework at hand