【问题标题】:C++ Error: Expected constructor, destructor, or type conversion before '(' tokenC++ 错误:'(' 标记之前的预期构造函数、析构函数或类型转换
【发布时间】:2014-05-10 02:02:14
【问题描述】:

我无法摆脱代码中的几个编译错误。我在程序的其他地方使用过类似的语法没有问题,所以我不确定是什么问题。

在 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:错误:在 'fName' 之前需要 ')'

PersonalRec.cpp: error: '(' token 之前的预期构造函数、析构函数或类型转换

我觉得他们是相关的。

编辑 - 第一个错误可以通过在 std::string fName 中添加字符串 fName 来修复,对于 lName 也是如此。该行的修改代码是

PersonalRec (std::string fName, std::string lName, Date bDate);

编辑 2 - 我对第二个错误做了同样的事情并且代码编译。

【问题讨论】:

    标签: c++


    【解决方案1】:

    我猜你需要

    #include <string> 
    

    在您的 .h 文件中,并且您需要在字符串前面加上 std::string 前缀。

    【讨论】:

    • 我会试一试。编辑 - 它摆脱了第一个错误,但第二个仍然存在。
    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 2016-03-14
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多