Error when define virtual method in C++

Hello,

How can I fix it?


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

class employee
{
public:
	virtual void Print();
};

virtual void employee::Print() // ERROR
{

}
int main()
{
	


	return 0;
}
What is the error message that you get?
Severity Code Description Project File Line Suppression State
Error (active) E0239 invalid specifier outside a class declaration C++_6 C:\Users\My\source\repos\C++_6\C++_6\Source.cpp 11
Fair enough, Visual Studio's error message is arguably worse than GCC's, which is "error: 'virtual' outside class declaration".

Anyway, the solution is to only put 'virtual' in the function declaration. Remove it from the actual definition.
Last edited on
Thank you,

Now I have an error elsewhere. (Line 48)

inherited member is not allowed

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include<string>
using namespace std;

class Student
{
private:
	int numberOfStudent;
	string family;
	float average;
	int entry;
	char gender;
public:
	Student(int num = -1, string f = "", float avg = 0, int en = 1390)
	{
		numberOfStudent = num;
		family = f;
		average = avg;
		entry = en;
	}
	char GetGender();
	void setInfo(int, string, float, int);
	int GetNumberOfStudent();
	string GetFamily();
	float GetAverage();
	int GetEntry();
	virtual bool authStu();
	virtual bool conditionalStu();
	virtual void Print();
};

class Undergraduate : public Student
{
private:
	bool takeProject;
public:
	Undergraduate(int num, string f, float avg, int en, bool project = false)
		: Student(num, f, avg, en)
	{
		takeProject = project;
	}
	void SetProject(bool);
	bool GetProject();

	
};

bool Undergraduate::authStu() // ERROR - inherited member is not allowed	
{
	int isAuth = 1399 - GetEntry();
	if (isAuth > 2)
	{
		cout << "Authorized Student";
		return true;
	}
	else
	{
		cout << "Unauthorized Student";
		return false;
	}
}
void Undergraduate::SetProject(bool project)
{
	takeProject = project;
}
bool Undergraduate::GetProject()
{
	return takeProject;
}


char Student::GetGender()
{
	return gender;
}

int Student::GetNumberOfStudent()
{
	return numberOfStudent;
}
bool Student::conditionalStu()
{
	if (GetAverage() < 12)
	{
		cout << "Conditional Student";
		return true;
	}
	else
	{
		cout << "Unconditional Student";
		return false;
	}
}
bool Student::authStu()
{
	int isAuth = 1399 - entry;
	if (isAuth > 4)
	{
		cout << "Authorized Student";
		return true;
	}
	else
	{
		cout << "Unauthorized Student";
		return false;
	}

}
void Student::Print()
{
	cout << "Number of Student: " << numberOfStudent << endl;
	cout << "Family: " << family << endl;
	cout << "Average: " << average << endl;
	cout << "Entry: " << entry << endl;
}

void Student::setInfo(int number, string f, float avg, int en)
{
	numberOfStudent = number;
	family = f;
	average = avg;
	entry = en;
}
int Student::GetEntry()
{
	return entry;
}
float Student::GetAverage()
{
	return average;
}
string Student::GetFamily()
{
	return family;
}


class Graduate
{

};


int main()
{


}
The actual error is :

48:29: error: no 'bool Undergraduate::authStu()' member function declared in class 'Undergraduate'


Always quote the entire error verbatim, not your interpretation of it.

So, should you delete the function lines 48 to 61, as you already have the same code on lines 94 to 108?

Or did you want to specialise that function - as you have it as virtual in the base class? If so, then it needs to be in the Undergraduate class definition with the override keyword.

By the way, the get functions should be marked const. Make sure you are consistent with this, as a const function is seen as a different function even though it has the same parameters.

The parameters should be const as well, and std::string should be passed by reference.

Functions like authStu shouldn't print output.

Rather than a function like setInfo, use the member intialise list like you have on line 38. Do that for all your constructors. Make sure to initialise all the members in the same order as in the class definition.

Have fun !! :+)
Last edited on
Topic archived. No new replies allowed.