学校作业。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 class车辆{ 朋友无效客人(); 私人: 串制; 弦模型; 国际年; 公众: 无效车辆(); 无效车辆(字符串、字符串、整数); void~车辆(); 字符串getMake(); } void guest(){ cout

学校作业。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 class车辆{ 朋友无效客人(); 私人: 串制; 弦模型; 国际年; 公众: 无效车辆(); 无效车辆(字符串、字符串、整数); void~车辆(); 字符串getMake(); } void guest(){ cout,c++,C++,构造函数和析构函数没有返回类型!应该是: Vehicle(); Vehicle(string, string, int); ~Vehicle(); 您需要向函数传递一个参数: void guest(const Vehicle &v) { cout << v.make; //friend allows you to access 'make' directly } 构造函数和析构函数没有返回类型。只需删除它们,代码就会编译。具有返回类型 void Vehicle(

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

Vehicle();
Vehicle(string, string, int);
~Vehicle();
您需要向函数传递一个参数:

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

构造函数和析构函数没有返回类型。只需删除它们,代码就会编译。具有返回类型

 void Vehicle(); 

告诉编译器您要声明名为Vehicle()的函数,但由于该函数与类同名,因此不允许声明该函数,除非它是构造函数(没有返回类型)。错误消息准确地告诉了您在这种情况下的问题所在。

如果您理解这些错误消息,那么在这种情况下,它们实际上是非常好的

void Vehicle();
因为“方法”与类同名,您的Intellisense认为它应该是构造函数。没错!构造函数没有返回类型,所以请:

Vehicle();
同样地:

void Vehicle(string, string, int);
也似乎是一个构造函数,因为“方法”的名称与类的名称相同。仅仅因为它有参数并不意味着它是特殊的。它应该是:

Vehicle(string, string, int);
~Vehicle();
解构器也没有返回类型,所以

void ~Vehicle();
应该是:

Vehicle(string, string, int);
~Vehicle();

构造函数和析构函数没有返回类型。删除“void”。构造函数和析构函数中的那些void真的在类的幻灯片中吗?是的,我觉得这个类真的很烂,因为给了我一张幻灯片来解释“friend”但事实并非如此work@StephanM只是他们注意到,他们试图用朋友的方式表明他们对基本C++一无所知。这比Cor和Dor的错误更严重,这可能只是一个疏忽。如果你可以或抱怨,就下课,虽然这不太好。我真的很讨厌说,我一直在。向学生系主任投诉的过程。好吧,这就解决了问题,但现在我在我的无效猜测中看到了以下内容。“1)智能感知:非静态成员引用必须与特定对象相关”,使用上面的朋友应该使“make”对来宾可见()。2)智能感知:成员“Vehicle::make”(在第16行声明)无法访问1)IntelliSense:no operator“感谢您发布完整的代码,但仍然在上得到一个错误,我根据给我的指示计算,由于没有返回任何内容,“void”是可以的。我现在看到的问题是来宾函数看不到“make”“@StephanM您无法通过这种方式访问该成员,因为您没有该类的实例。它不是静态成员,因此您需要为您的friend函数提供实例(或指向实例的指针或引用)。