作业.C++ Book Example似乎有问题

Ste*_*anM 7 c++

以下代码是我的C++类幻灯片的一部分.IntelliSence给了我错误,我不知道为什么.不知道为什么它不喜欢构造函数和析构函数.有人可以帮忙吗?

class Vehicle {
     friend void guest();
 private:
     string make;
     string model;
     int year;
 public:
     void Vehicle();
     void Vehicle(string, string, int);
     void ~Vehicle();
     string getMake();
 }

 void guest() {
     cout << make;
 }

 1) IntelliSense: member function with the same name as its class must be a constructor
 2) IntelliSense: member function with the same name as its class must be a constructor
 3) IntelliSense: return type may not be specified on a destructor
Run Code Online (Sandbox Code Playgroud)

And*_*rew 12

构造函数和析构函数没有返回类型!应该:

Vehicle();
Vehicle(string, string, int);
~Vehicle();
Run Code Online (Sandbox Code Playgroud)

您需要将参数传递给您的函数:

void guest(const Vehicle &v)
{
    cout << v.make; //friend allows you to access 'make' directly
}
Run Code Online (Sandbox Code Playgroud)

当然,您必须相应地更改friend声明

;在课堂结束时不要忘记

编辑

有效的完整代码:

class Vehicle {
    friend void guest(const Vehicle &v);
private:
    string make;
    string model;
    int year;
public:
    Vehicle() {}
    Vehicle(string make, string model, int year) : make(make), model(model), year(year) {}
    ~Vehicle() {}
    string getMake() const {return make;}
};

void guest(const Vehicle &v) {
    cout << v.make;
}



int main()
{
    guest(Vehicle("foo", "bar", 10));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)