表达式必须具有类类型
Expression must have a class type
我不想这是我的第一篇帖子,但我在这里不知所措。当我试图编译我的程序(应该只是简单地找到矩形的面积和周长)时,我一直会遇到这个错误。这是我的头文件。
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(float Lngth=1, float Wdth = 1);
void setLngth(float Lngth);
void setWdth(float Wdth);
float getLngth(float Lngth);
float getWdth(float Wdth);
void Perimeter(float lngth, float wdth);
void Area(float lngth, float wdth);
private:
float Lngth;
float Wdth;
};
这是我的.cpp文件。
#include <iostream>
using namespace std;
#include "RealRectangle.h" // Employee class definition
Rectangle::Rectangle(float Lngth, float Wdth)
{
&Rectangle::setLngth;
&Rectangle::setWdth;
}
void Rectangle::setLngth(float Lngth)
{
if((Wdth > 0.0) && (Wdth < 20.0))
float wdth = Wdth;
else
cout<<"Invalid Width."<<endl;
}
float Rectangle::getLngth(float Lngth)
{
return Lngth;
}
void Rectangle::setWdth(float Wdth)
{
if((Wdth > 0.0) && (Wdth < 20.0))
float wdth = Wdth;
else
cout<<"Invalid Width."<<endl;
}
float Rectangle::getWdth(float Wdth)
{
return Wdth;
}
void Rectangle::Perimeter(float lngth, float wdth)
{
cout<<"The Perimeter is "<<(2*(lngth + wdth));
}
void Rectangle::Area(float lngth, float wdth)
{
cout<<"The Area is "<<(lngth * wdth);
}
这就是我不断遇到错误的地方。编译器告诉我添加一个符号来创建指针,就像我在.cpp中所做的那样。但这本身就产生了另一个错误。等等。我不确定我做错了什么。错误出现在第10行和第11行。
#include <iostream>
using namespace std;
#include "RealRectangle.h"
int main()
{
Rectangle rectangle1();
Rectangle rectangle2();
cout<<rectangle1.Perimeter();
cout<<rectangle2.Area();
}
您遇到了被称为最麻烦的解析。
Rectangle rectangle1();
Rectangle rectangle2();
声明了两个函数,而不是两个对象。进行
Rectangle rectangle1;
Rectangle rectangle2;
此外,您可能应该将这些&Rectangle::setLngth
更改为函数调用。
矩形::周长()和矩形::面积()为void
类型。他们什么都不回。然而,您正试图使用它们不存在的返回值,并将其传递给cout
。
修改这两个函数,使它们返回一个值:
float Rectangle::Perimeter(float lngth, float wdth)
{
return 2 * (lngth + wdth);
}
float Rectangle::Area(float lngth, float wdth)
{
return lngth * wdth;
}
或者修改main()函数以简单地调用这些函数,因为现在它们已经打印到cout
:
int main()
{
Rectangle rectangle1;
Rectangle rectangle2;
rectangle1.Perimeter();
rectangle2.Area();
}
但你仍然有一个问题;这两个函数目前采用长度和宽度参数,我认为这不是您想要的。看起来您想要的是获得矩形对象的周长和面积。所以你应该使用类变量来计算。因此,省略这些参数,转而使用您的私人数据成员:
float Rectangle::Perimeter()
{
return 2 * (Lngth + Wdth);
}
float Rectangle::Area()
{
return Lngth * Wdth;
}
不要忘记更新头文件中类声明中的函数签名,而不仅仅是cpp文件中的实现。
此外,您的构造函数没有正确地委托初始化工作。函数调用的形式是function(arguments)
,而不是&function
。所以你需要做:
Rectangle::Rectangle(float Lngth, float Wdth)
{
setLngth(Lngth);
setWdth(Wdth);
}
最后,Rectangle
对象的声明被误解为函数原型:
Rectangle rectangle1();
Rectangle rectangle2();
编译器认为rectangle1
和rectangle2
是不带参数并返回矩形的函数。你应该省略括号:
Rectangle rectangle1;
Rectangle rectangle2;
我们还没有完成(天哪,这个程序有多少错误:-p)。您的setLngth
和setWdth
功能未按预期工作:
void Rectangle::setLngth(float Lngth)
{
if((Wdth > 0.0) && (Wdth < 20.0))
float wdth = Wdth;
else
cout<<"Invalid Width."<<endl;
}
仔细看一下。特别是说float wdth = Wdth;
的那一行。你的函数所做的是,取一个名为Lngth
的float
参数,然后检查Wdth
(私有变量)是否在范围内,如果是,则声明一个新的本地float
变量wdth
,并将其设置为与Wdth
相同的值。
该函数根本不初始化私有变量Wdth
。setWdth
函数也是如此。你也应该修理这些。
这:
Rectangle::Rectangle(float Lngth, float Wdth)
{
&Rectangle::setLngth;
&Rectangle::setWdth;
}
应该是这样的:
Rectangle::Rectangle(float Lngth, float Wdth)
{
setLngth(Lngth);
setWdth(Wdth);
}
这个:
Rectangle rectangle1();
Rectangle rectangle2();
应该是这样的:
Rectangle rectangle1;
Rectangle rectangle2;
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 表观调用前面的表达式必须具有指向 func 类型的指针
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- 表达式必须具有类类型 vs.
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 非类类型表达式的静态类型与动态类型之间的差异
- 如何确定涉及 C++ 中除法的算术表达式的数据类型
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 带有数组类型表达式的错误分配
- 表示函数参数的元组的类型表达式
- 如何确定撤销类型表达式的更大类型
- c++ 11中有新的函数类型表达式格式吗?