10
#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

Does this mean I have to use 2*square::length + 2*square::width?

1
  • 8
    Why would a square have a different length and width? You could just use side. Commented Mar 20, 2012 at 21:23

6 Answers 6

20

square is a type, not an object; instead of

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 please may not, prefer for clarity).

2*square::length + 2*square::width would be valid syntax if length and width were

  • static members of square, or
  • members of some base class square, or
  • objects in some namespace square
4

Yes, the accurate form would be:

return 2*square::length + 2*square::width;

since square is a type, not an object.

In this context, it's the same as:

return 2*this->square::length + 2*this->square::width;

However, since it's the same object and the same type, you can just write:

return 2*this->length + 2*this->width;

or, most simply:

return 2*length + 2*width;
3

Simply use

double square::perimeter() {
    return 2 * length + 2 * width;
}
3
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.

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:

double square::perimeter() {
    return 2 * length + 2 * width; //will refer to the instance variables automatically
}
2

to access instance variables, just name them:

double square::perimeter() {
  return 2*length + 2*width;
}
2
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();

so you perimeter function should be:

 double square::perimeter() {
    return (2*(length + width));
    }

But isnt the length and width of square equal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.