Pointers to functions and vectors within vectors

Hi,

I have created a class called Rows which has the following in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
#include "Berths.h"
#include "Ship.h"
class Rows
{
public:
Rows(void);
~Rows(void);

std::vector<Berths> _rows();

bool dock(Ship ship);

bool undock(Ship ship);

};


What I'm trying to is create a vector of rows that contains Berths.

And set up a bool to check whether the ship is docked or undocked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include <vector>
#include <iostream>
#include "Rows.h"
#include "Ship.h"

using namespace std; 
class Berths
{
public:
Berths(void);
~Berths(void);

vector<Rows> _berths;

Rows* dock(Ship ship);

Rows* undock(Ship ship);
};

This class should have a vector which has berths inside the rows.


However when I go to build this I get:
Error 1
error C2065: 'Rows' : undeclared identifier
Error 39 error C2065: 'Berths' : undeclared identifier
Error 14
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

They seem to be simple errors but I do not understand why they are occurring.

I have tried to take the approach which I was helped with last week but I am unsure what the problem was. From what I have been reading on pointers the syntax seems correct. Regarding my vectors I really do not understand how to put a vector inside a vector which seems to be the best way to approach it.

Thanks
Hi there! Vectors are template based, which means you can have nearly any parameter type you can think of, including vectors of things, for example:

vector<vector<vector<vector<int> > > > quadvec;

is valid, though a bit unique, the spaces between the >'s are important to prevent the compiler from thinking you're trying to use the bitwise shift or the stream operator >>.

However I don't see anything wrong with the code you've supplied.
I think its because you've included Rows in Berths and at the same time included Berths in rows. As far as I know that fucks it up.

Start by removing the void that is in the parameters of your constructor/desconstructor (dunno why they are even there)

Then, In the rows class, try removing #include "Berths.h" and std::vector<Berths> _rows(); and see how that goes.
Last edited on
is valid, though a bit unique, the spaces between the >'s are important

Only pre-C++11 compilers have this problem. With the current standard the spaces are not necessary.

You probably need rethink your classes. Why do you need a Berths in Rows? Why do you need a Rows in Berths? Why do you need a instance of these classes in each of the classes?

in rows.h line 10, remove the (). That makes it look like a function declaration.

You also have recursive #includes. rows.h includes berths.h and berths.h includes rows.h. When using vector<>, the type must be fully defined. If you can't fully define the type because of recursive includes, then one of them must use pointers.

When you run into recursive includes, it often means you don't have your class hierarchy correct. i.e. Why does Berths have a vector of Rows? One would think that a ship has a vector of rows (decks?). A row has a vector of berths, but an instance of the Berths class represents a single berth, not a vector of rows.

I'm unclear why both Berths and Rows have dock/undock functions. Isn't that an attribute of a Ship?



Last edited on
@PapaSmurf

I think a class hierarchy like this makes more sense:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Ship
{
    //stuff
};

class Berths
{
    Ship ship;
    //stuff
};

class Rows
{
    std::vector<Berths> _berths;
    //stuff
};

Rows would be the wharf/docks/piers while Berths would be a single dock/pier. A dock/pier can only hold one ship.
Hi,

Because the dock has 10 rows and each row has 10 berths. I need to be able to check the ship name within the row vector and berth vector.

This is why I have them linked together

Does my approach need assessing similar to what TarikNeaj and Jlb?

I'm guessing I just need the rows vector to hold berths and leave berths as a singular vector?

Honestly I pretty new to OO programming but it's something I need to learn.

Thanks for your help so far.
Then your class hierarchy should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Ship
{
    //stuff
};

class Berth
{
    Ship ship;
    //stuff
};

class Row
{
    std::vector<Berth> berths;
    //stuff
};

class Dock
{
    std::vector<Row> rows;
    //stuff
};


The way you had it in your opening post, assuming it actually worked, you would have a vector of Berths each containing a vector of Rows each containing a vector of Berths each containing a vector of Rows each containing a vector of Berths each containing a vector of Rows each containing a vector of Berths ad infinitum.
Topic archived. No new replies allowed.