Could use some help starting my dynamic array lab

Hey guys,

I've struggling with starting on my current lab which deals with dynamic arrays. I'll post what I have and explain why I'm doing it this way but if it's incorrect could someone chime in and tell me what I'm doing wrong?
Thanks!

So the prompt is pretty big so I'm taking it in chunks.
The first part is~
Write a new Checkbook class that uses a dynamic array.

So alright I've got this in my .h file
1
2
3
4
5
6
7
8
9
#ifndef Checkbook_h
#define Checkbook_h
class newCheckbook
{
public:
  newCheckbook(int size); // my dynamic array
private:
};
#endif 


Next~
Start off with a dynamic array size of 2 made by the constructor
so in my .cpp I've got

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Checkbook.h"

#include <iostream>
using namespace std;
int main()
{
  newCheckbook newCheckbook(int size)
  {
    int size = 2;
    array[size];
  };

}


However, on line 14 I'm getting an error. Does anyone know why?
int size = 2;
Last edited on
You can't define a function within another function. The definition of the function (it is a constructor) should go in the Checkbook.cpp file.

Although not illegal, I would steer away from naming an object the same as it's class.

Hope all is well.

Wait I'm a bit confused because I'm not sure where I'm defining a function within another function. In my .h file I have the function prototype for my constructor which is telling my program I want to pass in an int called size. In my .cpp I pass into the constructor the value of 2.

What about if I did something like this?

1
2
3
4
5
6
7
8
9
10
#ifndef Checkbook_h
#define Checkbook_h
class newCheckbook
{
public:
  newCheckbook(); // my dynamic array
private:
int size = 2;
};
#endif  


.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Checkbook.h"

#include <iostream>
using namespace std;
int main()
{
  newCheckbook newCheckbook()
  {
    int size = 2;
    array[size];
  };

}


I still get an error on int size = 2; it says use of undefined identifier. All I'm trying to do is make a dynamic array size of 2 made by the constructor but my program just isn't working :(
Last edited on
Lines 7 to 11 in the main.cpp file are a function definition inside the main() function.

As I said, put the definition of the constructor into the Checkbook.cpp file.

Line 10 is an error because you haven't said what type variable is, int presumably?

Don't set the value of size in the header file, do it in the Checkbook.cpp file. Don't re-declare it either.

Make sure to create an object of type Checkbook in main(). You will probably want to add a function to the the class that cout's something, so that you can see what is happening. Also need some interface functions so you can interact with your object.

Also read this:

http://www.cplusplus.com/doc/tutorial/


And be aware of all the reference info, articles etc on this site as well.

Don't set the value of size in the header file, ...

Of course, if you're coding with C++11, this is now valid.

(But the handling of the "array" member needs some work!)

Andy

#include <iostream>
#include <vector>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef Checkbook_h
#define Checkbook_h
class newCheckbook
{
public:
  newCheckbook();
  int getSize()
  {
    return size;
  }
private:
  int size = 2;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

#include "Checkbook.h"

newCheckbook::newCheckbook()
{
}

int main()
{
  newCheckbook cbk;
  cout << cbk.getSize() << endl;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.