Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Header file help

Name: Anonymous 2009-03-26 17:24

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];

    public:
    Foo();
};

=====foo.cpp=====
class Foo
{
    public:
    Foo::Foo()
    {
        MAX_ENTRIES = 25;
    }
}

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: Anonymous 2009-03-26 17:26

EXPERT PROGRAMMING TUITION

Name: Anonymous 2009-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.

Name: Anonymous 2009-03-26 17:29

>>1
malloc

Name: Anonymous 2009-03-26 17:35

>>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: Anonymous 2009-03-26 17:36

whatever sepples standards say, I find the practice of not declaring constants scalars in the header file PIG DISGUSTING

Name: Anonymous 2009-03-26 17:37

>>6
fucking me, don't ever confuse declaration with definition anymore, capische?

Name: Anonymous 2009-03-26 17:48

>>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: Anonymous 2009-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: Anonymous 2009-03-26 17:59

>>8
I don't get what you're saying. Could you try to be a little bit more clear?

Name: Anonymous 2009-03-26 18:08

>>5
Sorry. In >>3, s/header file/cpp file/

Name: Anonymous 2009-03-26 19:09

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: Anonymous 2009-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

Name: ​​​​​​​​​​ 2010-10-23 3:35

Name: Anonymous 2011-02-04 15:56

Name: Anonymous 2013-01-19 23:07

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List