在类中嵌套对象 < P>使用C++,我试图将一个类的对象嵌套在另一个类中,在代码6行的语法错误> CarpetClass .H 中表示
错误:函数“矩形”不是类型名称 myclass.h在类中嵌套对象 < P>使用C++,我试图将一个类的对象嵌套在另一个类中,在代码6行的语法错误> CarpetClass .H 中表示,c++,class,syntax-error,inner-classes,C++,Class,Syntax Error,Inner Classes,错误:函数“矩形”不是类型名称 myclass.h class Rectangle{ private: double length; double width; public: void setLength(double len){ length = len; } void setWidth(double wid){ width = wid; } double getLength(){ ret
class Rectangle{
private:
double length;
double width;
public:
void setLength(double len){
length = len;
}
void setWidth(double wid){
width = wid;
}
double getLength(){
return length;
}
double getWidth(){
return width;
}
double getArea(){
return length*width;
}
};
#include "myclass.h"
class Carpet{
private:
double pricePerSqYd;
Rectangle size;
public:
void setPricePeryd(double p){
pricePerSqYd = p;
}
void setDimensions (double len, double wid){
size.setLength(len / 3);
size.setWidth(wid / 3);
}
double getTotalPrice(){
return (size.getArea*pricePerSqYd);
}
};
class.h
class Rectangle{
private:
double length;
double width;
public:
void setLength(double len){
length = len;
}
void setWidth(double wid){
width = wid;
}
double getLength(){
return length;
}
double getWidth(){
return width;
}
double getArea(){
return length*width;
}
};
#include "myclass.h"
class Carpet{
private:
double pricePerSqYd;
Rectangle size;
public:
void setPricePeryd(double p){
pricePerSqYd = p;
}
void setDimensions (double len, double wid){
size.setLength(len / 3);
size.setWidth(wid / 3);
}
double getTotalPrice(){
return (size.getArea*pricePerSqYd);
}
};
Source.cpp
#include <iostream>
#include "CarpetClass.h"
using namespace std;
int main(){
Carpet purchase;
double pricePerYd;
double length;
double width;
cout << "Room length in feet: ";
cin >> length;
cout << "Room width in feet: ";
cin >> width;
cout << "Carpet price per sq. yard: ";
cin >> pricePerYd;
purchase.setDimensions(length, width);
purchase.setPricePeryd(pricePerYd);
cout << "\nThe total price of my new " << length << "x" << width << " carpet is $" << purchase.getTotalPrice() << endl;
}
#包括
#包括“类别h”
使用名称空间std;
int main(){
地毯采购;
双倍价格;
双倍长度;
双倍宽度;
长度;
宽度;
普利斯珀伊德;
采购。设置尺寸(长度、宽度);
购买。定价码(pricePerYd);
库特
class Rectangle
将使编译器理解您指的是类的名称,而不是“使用设备上下文绘制矩形的Windows函数”
使用名称空间来避免名称冲突是一种很好的做法。或者,也可以使用类似“用C前缀一个类名”的约定,即class CRectangle{…
,然后使用以下非常简单的驱动程序代码,它将不会与类似函数的名称冲突:
#include "CarpetClass.h"
int main()
{
Carpet c;
}
代码在Linux下使用gcc干净地编译,但前提是我修复了成员函数:
double getTotalPrice(){
return (size.getArea()*pricePerSqYd);
}
您是否确保没有包含任何其他定义矩形的内容
?您可能还希望在标题中插入一些#include
保护,并提供默认构造函数和析构函数
要问一个更好的问题,下次尝试直接从您的计算机剪切和粘贴代码(以及错误消息!),以避免浪费时间的打字错误。您应该研究.h和.cpp文件之间的差异。我看到的唯一问题是size.getArea*pricePerSqYd
应该是size.getArea()*pricePerSqYd
,除此之外,一切都应该可以正常编译。您没有将myclass.h
设置为包含FuelClass.h
是吗?我非常怀疑您的编译器在错误消息中是否使用了“fuction”一词。请完整发布实际的错误消息。当我将鼠标悬停在矩形上时,它会显示:BOOL_stdcall Rectangle(HDC HDC,int left,int top,int right,int bottom)错误:函数“Rectangle”不是类型名称"在Rectangle
之前,使用typename
、struct
和class
中的一种来消除歧义。谢谢!这是有效的。在教科书中没有这样做,我不知道它们是如何没有出错的。@user3786266:很简单,它们在作用域中也没有使用相同名称的函数声明。BOOL\u stdcall Rectangle(HDC-HDC,int-left,int-top,int-right,int-bottom)
是该名称与Windows功能冲突的泄露