Defining object/array within a class

Edit: Must have been sleepy, I just now realized my Vector.h file did not include Student, which seems to have caused the problem. My code now compiles correctly (for now).

-----------------------------------------------------
I have a question on where it's valid to define an object in C++. I am trying to define a dynamic array of dynamic objects that would be accessible to a class Vector. The problem seems to be in my file Vector.h. I have another class called Student that's currently empty that I am using to create the objects.

If I try to initiate the line Student* *list; in my Vector.h file, I am given the errors below.

Errors:
vector.h(14): error C2143: syntax error : missing ';' before '*'
vector.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Vector.h File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef VECTOR_H
#define VECTOR_H

class Vector {

public:
	
	Vector();
	int getCapacity();
	
private:
	int capacity;
	int size;
	Student* *list; // Line that causes the error if defined here
};
#endif 


Vector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Vector.h"
#include "Student.h"
#include <iostream>

using namespace std;

Vector::Vector()
{
        capacity = 2;
	size = 0;
	list = new Student*[capacity];
}

int Vector::getCapacity()
{
	return capacity;
}


-------------------------------------------------------------------

The alternative is that I have been initiating this code under Vector::Vector(), and this compiles without error, but the array list is no longer accessible from other methods in the class that I will be adding later. What's the correct way to initiate this dynamic array / objects so that they are accessible by all public functions in the class?
Last edited on
Vector.h has no knowledge of what a Student is. You need to either include Student.h in it, or forward declare Student. See http://www.cplusplus.com/forum/articles/10627/ (you may want to start from section 4).
Topic archived. No new replies allowed.