Problem with Nested Classes

Sep 15, 2020 at 4:24pm
Hey guys, it's been over a year since i've used C++ or done much programming at all, so i apologize for my stupid question but i feel i'm just stuck on some basic stuff here. I'm writing a program to record online class sessions for a professor, I need to be able to create Class sections with a maximum of 50 students.

What i'm trying to do is my make an array of 50 students from the 'Student' Class a Member of the 'ClassSection' Class, is this possible or a stupid way to do it? I'm getting errors when i try to compile as is. Can anyone see a better way to go about this or a way to fix my syntax errors? Thanks for any help you can provide!

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "ClassSection.h"
#include "ClassSection.h"
#include <sstream>
using namespace std;
class ClassSection
{
public:
	string className;
	Student student[];

};
class Student
{
	public:
	string firstName;
	string lastname;
	int numSkips;
	int numExceptional;
	int numSatisfactory;
	int numUnsatisfactory;

};
int main()
{
	ClassSection section1;
	// Student students[50];
	cout << "Please enter your class name: ";
	cin >> section1.className;
	string filename, studentname, line;
	cout << "Please enter your filename: ";
	cin >> filename;
	int x = 0;
	fstream MyReadFile(filename);
	while (getline(MyReadFile, line))
	{
		string data;
		stringstream linestream(line);

		getline(linestream, data, '\t');

		linestream >> section1.student[x].firstName >> section1.student[x].lastname;
		x++;
	}
	int size = sizeof(section1.student[x]) / sizeof(section1.student[0]);
	int chooseStudent = rand() % x;
	cout << section1.student[chooseStudent].firstName << " " << section1.student[chooseStudent].lastname;
	cout << "\n";
	cout << "Select your option from the list: ";
	cout << "1. Skip" << "\n" << "2. Mark Excellent"
		<< "\n" << "2. Mark Satisfactory" << "\n" << "3. Mark Unsatisfactory";


}
Last edited on Sep 15, 2020 at 4:25pm
Sep 15, 2020 at 5:06pm
You're trying to use Student in the class ClassSection, but at that point, the compiler has no idea what a Student is.

Put the definition of Student BEFORE ClassSection.
Sep 15, 2020 at 6:56pm
no nested class there
1
2
3
4
class foo{
   class nested{ //this class is defined inside the other
   };
};
an example would be vector<int>::iterator
think about their relationship

> make an array of 50 students
> Student student[];
perhaps you should consider to write a number (for example 50) between those brackets
Sep 15, 2020 at 7:28pm
I need to be able to create Class sections with a maximum of 50 students.


Why specify a limit using an array? Why not use a vector?

 
vector<Student> student;

Sep 16, 2020 at 3:28am
Depends on what you are after. For a constructive flow try inheritance.

Inheritance - Relied solely on calling other classes from the parent class.

For example class Student : ClassSection , uses the parent class (ClassSection), their functions and variables.

In main, Student can create an object that calls all public variables from ClassSection. Instead of using ClassSection section1, it could be Student section1 .

Just my two cents.
Topic archived. No new replies allowed.