Classes

closed account (EAp4z8AR)
How would I define non static member functions from classes? I keep getting an error saying that first is not a static member if 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <fstream>
#include <string>

using namespace std;

class Student
{
    public:
    string first;
    char middle;
    string last;
    long double id;
    string username;
};

class Course
{
    public:
    string course;
};

class Lecture
{
    public:
    string day;
    int start;
    int duration;
    bool lab;
};


string Student::first
{
    
}
Hi,

The compiler is trying to interpret this as a function definition - it doesn't work: first is a variable.

To access the string first, you need an instance of the class, inside main:

Student TheStudent;

then assign it a value:

TheStudent.first = "Fred";

You shouldn't have public data, make them private, and provide interface functions to manipulate them.

You should have constructors for your classes.

Don't #include files you don't need.

#include <cmath> not math.h

Good Luck !!
Last edited on
Topic archived. No new replies allowed.