Problems with vector of classes

Hi everyone,
I'm trying to make a system that manage schools (with students, classes, teachers, ect). I'm having problems trying to create a vector of school classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class schClass //school class
{
public:
	schClass(void);
	schClass(int year,char section);
	~schClass();
	void year(int year){ pyear = year; }
	const int year(void){ return pyear; }
	void section(char section){ psection = section; }
	const char section(void){ return psection; }
	friend ostream& operator<<(ostream& os, const schClass& sC);
private:
	int pyear;
	char psection;
};

  class school
{
public:
	school();
	~school();
	void type(typeOfSchool type);
	const string type(void);
	void name(string name);
	const string name(void);
	void location(string location);
	const string location(void);
	void freeSat(bool freeSat);
	const bool freeSat(void);
	const int numOfStuds(void); 
	const double meanMark(void); 
	const int meanWage(void);
	void addClass(schClass& newClass); //C2061
private:
	typeOfSchool ptype = typeOfSchool::Unknown;
	string pname = "Unknown";
	string plocation = "Unknown";
	bool pfreeSat = UNKNOWN;
	int pnumOfStuds = UNKNOWN;
	double pmeanMark = UNKNOWN;
	int pmeanWage = UNKNOWN;
	vector<schClass> sections; //C2065 & C2923
};


I get these errors

1
2
3
4
5
6
Error	1	error C2061: syntax error : identifier 'schClass'		

Error	2	error C2065: 'schClass' : undeclared identifier		

Error	3	error C2923: 'std::vector' : 'schClass' is not a valid template type argument for parameter '_Ty'


The two classes are in different files and I've already included <vector> and put "using namespace std;"
Last edited on
have you
 
#include<vector> 

?

and then you need to say std::vector<etc etc>

regarding the first 2, you might be better off putting the classes in separate files.
It looks as though you haven't included the definition for schClass in whatever file is giving you those errors.
I've included my "schClass.h"
These are the whole files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once
#include"Include.h"

class school
{
public:
	school();
	~school();
	void type(typeOfSchool type);
	const string type(void);
	void name(string name);
	const string name(void);
	void location(string location);
	const string location(void);
	void freeSat(bool freeSat);
	const bool freeSat(void);
	const int numOfStuds(void); //calculate Number of students
	const double meanMark(void); //calculate Mean Mark
	const int meanWage(void);
	void addClass(schClass& newClass);
private:
	typeOfSchool ptype = typeOfSchool::Unknown;
	string pname = "Unknown";
	string plocation = "Unknown";
	bool pfreeSat = UNKNOWN;
	int pnumOfStuds = UNKNOWN;
	double pmeanMark = UNKNOWN;
	int pmeanWage = UNKNOWN;
	vector<schClass> sections;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once

//SDT LIBS

#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>
#include<stdexcept>
#include<vector>

//USING

using namespace std;

//CUSTOM LIBS

#include"enum.h"
#include"constants.h"

//CLASSES

#include"schClass.h"
#include"school.h"
#include"person.h" 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include"Include.h"

class schClass
{
public:
	schClass(void);
	schClass(int year,char section);
	~schClass();
	void year(int year){ pyear = year; }
	const int year(void){ return pyear; }
	void section(char section){ psection = section; }
	const char section(void){ return psection; }
	friend ostream& operator<<(ostream& os, const schClass& sC);
private:
	int pyear;
	char psection;
};
Solved, I had to declare that was a class

 
void addClass(class schClass& newClass);
that's not right..
What's right then?
It works in this way
If

void addClass(class schClass& newClass);

works but

void addClass(schClass& newClass);

doesn't then you can't be defining class schClass before class School.

The class in class schClass& newClass means this member function declaration line is also providing a forward declaration of the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;

class Test {
public:
    void check(class Other o); // foward declaration of class Other
};

class Other { // class definition
private:
    int i;
public:
    Other(int j) : i(j) {}
    int geti() const { return i; }
};

void Test::check(class Other o) {  // method definition
    cout << o.geti() << "\n";
}

int main() {
    class Test t; // "class" is not needed here
    class Other o(3); // or here
    t.check(o);
    return 0;
}


Forward declaration for classes are normally written like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

class Other; // foward declaration of class Other

class Test {
public:
    void check(Other o);
};

class Other { // class definition
private:
    int i;
public:
    Other(int j) : i(j) {}
    int geti() const { return i; }
};

void Test::check(class Other o) {  // method definition
    cout << o.geti() << "\n";
}

int main() {
    Test t;
    Other o(3);
    t.check(o);
    return 0;
}


Andy


Last edited on
Thank you, I solved including "schClass.h" in "school.h" (it was in "Include.h")

Anyway I don't know why, any suggestion?
I can see you have circular link dependencies - school.h includes Include.h, and Include.h includes school.h. That may be the cause of the problem. In any case, it indicates that you haven't thought clearly about the design.

In general, trying to include every header file in every translation unit is a bad idea. You want to keep your dependencies as tidy and stripped-down as possible. If A.h needs to include B.h, it should include only B.h, not every other header in your project.
I thought that using "#pragma once" or "#ifndef/#define/#endif" would have solved every problem but it doesn't seem so, thank you for the advice.
#pragma once (or the older-style include guards) solve a specific problem. They don't magically vanish away all problems caused by circular dependencies.
Last edited on
Topic archived. No new replies allowed.