gpt4 book ai didi

c++ - 类构造函数错误,类 "classname"没有成员 "classname"

转载 作者:行者123 更新时间:2023-11-28 03:24:12 28 4
gpt4 key购买 nike

#ifndef RESERVATIONS_H_INCLUDED
#define RESERVATIONS_H_INCLUDED
#include <vector>
#include <string.c>

class Reservations
{
public:
Reservations::Reservations { }
Reservations(string FullName, int PhoneNum);
string getname() { return FullName; }
int getnumber() { return PhoneNum; }
private:
string FullName;
int PhoneNum;
}

#endif // RESERVATIONS_H_INCLUDED

Reservations::Reservations(string FullName, int PhoneNum) //error on this line
{
FullName = FullName;
PhoneNum = PhoneNum;
}

我在标题中得到了错误,我不知道为什么它假设我希望它有一个属于它自己的类的成员......

最佳答案

你包含了错误的头文件

改变

#include <string.c>

#include <string>

也可以使用带有完整命名空间标准的字符串。

下面的代码应该通过小的修复来编译:

class Reservations
{
public:
Reservations() : PhoneNum(0) {}
Reservations(std::string FullName, int PhoneNum);
std::string getname() { return FullName; }
int getnumber() { return PhoneNum; }
private:
std::string FullName;
int PhoneNum;
};

Reservations::Reservations(std::string FullName, int PhoneNum)
{
this->FullName = FullName;
this->PhoneNum = PhoneNum;
}

// Better use member initializers list
Reservations::Reservations(std::string FullName, int PhoneNum)
: FullName(FullName),
PhoneNum(PhoneNum)
{
}

关于c++ - 类构造函数错误,类 "classname"没有成员 "classname",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com