C++ 错误:在"("令牌之前进行预期的构造函数、析构函数或类型转换

C++ Error: Expected constructor, destructor, or type conversion before '(' token

本文关键字:构造函数 析构函数 类型转换 错误 令牌 C++      更新时间:2023-10-16

我在处理代码中的几个编译错误时遇到了麻烦。我在程序的其他地方使用了类似的语法,没有问题,所以我不确定是哪里出了问题。

在PersonalRec.h:

#ifndef PersonalRec_H
#define PersonalRec_H
class PersonalRec
{
public:
    PersonalRec ();
    PersonalRec (string fName, string lName, Date bDate); //This line shows the first error
protected:
    void displayPersonalRec() const;
    int getAgeInYears() const;
private:
    std::string FirstName;
    std::string LastName;
    Date DoB;
};
#endif
在PersonalRec.cpp:

#include<iostream>
#include<string>
#include<math.h>
#include "Date.h" //contains prototypes for Date class
#include "PersonalRec.h"
extern Date currentDate;
PersonalRec::PersonalRec()
{
}
PersonalRec::PersonalRec(string fName, string lName, Date bDate) //This line shows the second error
{
    FirstName = fName;
    LastName = lName;
    DoB = bDate;
    displayPersonalRec();
}
//Implementations of protected methods follow

编译器错误读取

PersonalRec.h: error: expected ')' before 'fName'

PersonalRec.cpp:错误:在'(' token

之前期望构造函数、析构函数或类型转换

我有一种感觉他们是有关系的。

EDIT -第一个错误可以通过将string fName前缀为std::string fName和lName前缀来修复。该行修改后的代码是

PersonalRec (std::string fName, std::string lName, Date bDate);
编辑2 -我对第二个错误做了同样的事情,代码编译。

我猜你需要

#include <string>