C++ '.' 之前的预期主要表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9794328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected primary expression before '.'
提问by photon
#include <iostream>
using std::cout;
using std::endl;
class square {
public:
double length, width;
square(double length, double width);
square();
~square();
double perimeter();
};
double square::perimeter() {
return 2*square.length + 2*square.width;
}
int main() {
square sq(4.0, 4.0);
cout << sq.perimeter() << endl;
return 0;
}
I'm trying to write a simple class program. I am getting the error
我正在尝试编写一个简单的类程序。我收到错误
in member function 'double square::perimeter()'; .cpp:21: error: expected primary-expression before '.' token .cpp:21: error: expected primary-expression before '.' token
在成员函数'double square::perimeter()'中;.cpp:21: 错误:'.' 之前的预期主表达式 标记 .cpp:21: 错误:'.' 之前的预期主表达式 令牌
Does this mean I have to use 2*square::length + 2*square::width??
这是否意味着我必须使用 2*square::length + 2*square::width?
回答by ildjarn
square
is a type, not an object; instead of
square
是一个类型,而不是一个对象;代替
return 2*square.length + 2*square.width;
do
做
return 2*length + 2*width;
(which is implicitly the same as:
(这与以下内容隐含相同:
return 2*this->length + 2*this->width;
which you may, or pleasemay not, prefer for clarity).
你可以,或者请可能不会,更喜欢清晰)。
2*square::length + 2*square::width
would be valid syntax if length
and width
were
2*square::length + 2*square::width
如果length
并且width
是,将是有效的语法
- staticmembers of
square
, or - members of some base class
square
, or - objects in some namespace
square
- 的静态成员
square
,或 - 某些基类的成员
square
,或 - 某个命名空间中的对象
square
回答by Lightness Races in Orbit
Yes, the accurate form would be:
是的,准确的形式是:
return 2*square::length + 2*square::width;
since square
is a type, not an object.
因为square
是类型,而不是对象。
In this context, it's the same as:
在这种情况下,它与以下内容相同:
return 2*this->square::length + 2*this->square::width;
However, since it's the sameobject and the sametype, you can just write:
但是,由于它是相同的对象和相同的类型,你可以只写:
return 2*this->length + 2*this->width;
or, most simply:
或者,最简单的是:
return 2*length + 2*width;
回答by Gordon Gustafson
double square::perimeter() {
return 2*square.length + 2*square.width;
}
You need to say square::perimeter()
because you are defining a method of the square class itself. It may seem like you want to define it on a specific object, but you want it to be available to all instances of square, so you need to define it on a specific one.
你需要说,square::perimeter()
因为你正在定义square类本身的一个方法。看起来您想在特定对象上定义它,但您希望它可用于所有 square 实例,因此您需要在特定对象上定义它。
The instance variables length
and width
on the other hand, pertain to a specific instance of a class and NOT the entire class itself (otherwise you would declare them as static
). This means that you can just refer to them directly instead of going through the class itself, and the compiler will know what variables you're talking about. This is because width
and length
are defined in the same scope as the method, so you don't need to give it special directions with ::
to tell it where to find what its looking for. Hence:
实例变量length
,并width
在另一方面,属于一类的特定实例而不是整个类本身(否则你将宣布他们为static
)。这意味着您可以直接引用它们而不是通过类本身,并且编译器将知道您在谈论哪些变量。这是因为width
和length
定义在与方法相同的范围内,所以您不需要给它特殊的指示::
来告诉它在哪里可以找到它正在寻找的东西。因此:
double square::perimeter() {
return 2 * length + 2 * width; //will refer to the instance variables automatically
}
回答by Bart
Simply use
只需使用
double square::perimeter() {
return 2 * length + 2 * width;
}
回答by Karoly Horvath
to access instance variables, just name them:
要访问实例变量,只需命名它们:
double square::perimeter() {
return 2*length + 2*width;
}
回答by Rohit Vipin Mathews
double square::perimeter() {
return 2*square.length + 2*square.width;
}
what is the square
in this function? square is a class. you use the .
operator to acces members from objects. like sq.somefun();
什么是square
这个功能呢?square 是一个类。您可以使用.
运算符访问对象中的成员。喜欢sq.somefun();
so you perimeter function should be:
所以你的周长函数应该是:
double square::perimeter() {
return (2*(length + width));
}
But isnt the length and width of square equal?
但是正方形的长和宽不一样吗?