类构造函数错误,类“classname”没有成员“classname”

时间:2013-01-28 22:49:24

标签: c++ class constructor header

#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;
}

我在标题中收到错误,我不知道为什么它假设我希望它拥有自己类的成员......

3 个答案:

答案 0 :(得分:2)

你包含错误的头文件

更改

#include <string.c>

#include <string>

还要使用带有完整命名空间std。

的字符串

下面的代码应该使用次要修复进行编译:

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)
{
}

答案 1 :(得分:0)

你的意思是

Reservations::Reservations() { }

而不是

Reservations::Reservations { }

答案 2 :(得分:0)

这样做:

class Reservations
{
    public:
    //Reservations::Reservations { }
    Reservations() { }
    ....