编译器错误:未在作用域中声明
Compiler error: not declared in the scope
我正在尝试为我创建的几个类测试我的代码,但在尝试编译时遇到了一些困难。我有三节课和一节主课。第一类是人类,第二类和第三类是从人衍生出来的学生和教师。以下是我为Person.h类提供的内容:
#ifndef Person_H
#define Person_H
#include <string>
using namespace std;
class Person
{
protected:
long id;
string name;
string email;
string address;
string dateOfBirth;
string gender;
public:
Person(long pId);
Person(long pId, string pName, string pEmail, string pAddress, string pDateOfBirth, string pGender);
void print() const;
};
#endif
这是我的人。C
#include <iostream>
#include "Person.h"
using namespace std;
Person::Person(long pId)
{
id = pId;
name = "Nothing";
email = "Nothing";
address = "Nothing";
dateOfBirth = "Nothing";
gender = "Nothing";
}
Person::Person(long pId, string pName, string pEmail, string pAddress, string pDateOfBirth, string pGender)
{
id = pId;
name = pName;
email = pEmail;
address = pAddress;
dateOfBirth = pDateOfBirth;
gender = pGender;
}
void Person::print() const
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Email: " << email << endl;
cout << "Address: " << address << endl;
cout << "Date of Birth: " << dateOfBirth << endl;
cout << "Gender: " << gender << endl;
}
这是Student.h
#ifndef Student_H
#define Student_H
#include<string>
#include <vector>
#include "Person.h"
#include "Course.h"
using namespace std;
class Student:public Person
{
protected:
int yearOfStudy;
string major;
long advisorId;
vector <Course> coursesTaken;
static long nextStId;
public:
Student();
Student(string sName, string sEmail, string sAddress, string sDateOfBirth, string sG
ender, int sYearOfStudy, string sMajor, long sAdvistorId);
void print() const;
};
#endif
这是学生.C
#include<iostream>
#include "Student.h"
using namespace std;
long Student::nextStId = 500;
Student::Student():Person(nextStId)
{
yearOfStudy = 0;
major = " ";
advisorId = 0;
}
Student::Student(string sName, string sEmail, string sAddress, string sDateOfBirth, st
ring sGender, int sYearOfStudy, string sMajor, long sAdvisorId):Person(nextStId, sName
, sEmail, sAddress,sDateOfBirth, sGender)
{
yearOfStudy = sYearOfStudy;
major = sMajor;
advisorId = sAdvisorId;
}
void Student::print() const
{
Person::print();
cout << "Year of study: " << yearOfStudy << endl;
cout << "Major: " << major << endl;
cout << "Advisor ID: " << advisorId << endl;
}
现在,我主要有:
#include <iostream>
#include <string>
#include "Faculty.h"
#include "Student.h"
#include "Person.h"
using namespace std;
int main()
{
Student test1;
Faculty test2;
test1.print();
cout << endl;
test2.print();
return 0;
}
当我尝试编译时收到的错误是:
g++ Assignment3.C Person.C Student.C Faculty.C
Assignment3.C: In function âint main()â:
Assignment3.C:10: error: âStudentâ was not declared in this scope
Assignment3.C:10: error: expected `;' before âtest1â
Assignment3.C:12: error: âtest1â was not declared in this scope
我不明白为什么它会给我那个错误。我的教员班似乎运行得很好。有人能帮忙吗!
我不是专家,但测试1后你不需要()吗?像学生测试1();我知道这与错误无关,但你可以检查一下。
相关文章:
- 未在作用域中声明unordered_map
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 未在此作用域中声明的函数和变量 (C++)
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 重载时未在 C++ 的作用域错误中声明
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- Tictactoe 游戏变量未在作用域中声明
- 当变量在多个函数作用域中使用时,我应该在类 private 中声明该变量吗?
- 如何在C++中嵌套词法作用域可访问的作用域中声明静态信息?
- gets()未在作用域中声明
- 在C++14中,在哪个作用域中声明了重新声明枚举的非范围枚举器
- C++11 外部作用域变量声明为 auto
- 未在此作用域中声明类 (c++)
- 如何声明在函数作用域中定义的结构
- 未在此作用域中声明函数,即使存在头文件也是如此
- 变量声明中结构的作用域解析是什么意思
- 在C++中,在给定的相同作用域内声明相同的变量名
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 类未在作用域中声明,但已声明该类
- 使用新的外部函数作用域声明和初始化变量