【问题标题】:Compiler error: not declared in the scope编译器错误:未在范围内声明
【发布时间】:2015-11-16 18:06:40
【问题描述】:

我正在尝试为我创建的几个类测试我的代码,但在尝试编译时遇到了一些困难。我有三个班级和一个主班。第一类是人类,第二和第三类是从人派生的学生和教师。 这是 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

这是我的 Person.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

这是Student.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

我不明白为什么它给了我这个错误。我的教师课似乎运行得非常好。有人可以帮忙吗!

【问题讨论】:

  • 你在哪里声明Student
  • @user3508309 如果错误消息都是关于Student 的,你为什么要发布Person 类?
  • 好吧 student 是从 person 类派生的,所以我想我不妨把它贴出来,以防万一那里出了什么问题。
  • 我无法重现您的问题。复制粘贴您提供给我们的代码,注释掉提到 CourseFaculty 的行(因为您没有提供它们的源代码),一切都编译并运行良好。
  • 除非您没有向我们展示代码中的其他错误,或者您没有展示您尝试构建的实际代码,否则没有任何东西可以解释您遇到的错误。跨度>

标签: c++ inheritance compiler-errors


【解决方案1】:

我不是专家,但你不需要在test1之后有()吗?像学生 test1();我知道这与错误无关,但您可以检查。

【讨论】:

  • 不,这是一个函数声明。
  • 这应该是评论,但实际上并没有回答问题。
  • 我无法发表评论,因为我的声望低于 50。 () 当你也有构造函数时使用它。所以如果你有{ class Class { Class (int x) { } } int main () { Class test (10); }
  • @Konowy 但是,当您调用不带参数的构造函数时,您在键入() 时会遇到问题。请阅读“最令人烦恼的解析”。
  • 我从来没有使用过objective c++,我认为它就像在C#或Java中一样。
猜你喜欢
  • 2011-10-08
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多