Jump to content

Hi guys, I'm a beginning programmer in school, and this error is making me terror my hair out. We're learning of classes and such in c++ and I'm assigned to making a rectangle.

I have a member function called setlength() which is supposed to set length to the double len, which the client code inputs, however it keeps telling me that it is invalid use of member function. Any help?

 

 

// function declaration (in its own h file with rectangle class etc declared properly as far as I know)

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include <iostream>
#include <iomanip>

using namespace std;

class rectangle
{
public:
    void setlength(double len);
    void setwidth(double wid);
    double perimeter();
    double area();
    void show();
    double length();
    double width();


private:


};
#endif

// function definition

#include "rectangle.h"

void rectangle::setlength(double len)
{
   length = len;
}

void rectangle :: setwidth(double wid)
{
width = wid;
}
double rectangle :: perimeter()
{
perimeter = length + length + width + width;
return perimeter;
}
double rectangle :: area()
{
area = legnth * width;
return area;
}
void show()
{
return length;
return width;
}

Thanks in advance

rubber dome apologist

Link to comment
https://linustechtips.com/topic/771160-help-with-school-program-c/
Share on other sites

Link to post
Share on other sites

I'm not seeing variable declarations. Or am I going insane?

 

EDIT: Okay, since this is OOP land. I'm not seeing any declarations of members. (function names in classes are called methods)

Link to post
Share on other sites

2 minutes ago, fizzlesticks said:

You declared length() as a member function but you're trying to assign to it as if it were a variable.

Thank you! I realize it's a dumb mistake but as I'm discovering programming is not my forte lol

rubber dome apologist

Link to post
Share on other sites

2 minutes ago, fizzlesticks said:

In C++ they're called member functions.

I'll just go back to variables and functions then.

 

Either way, I don't see declarations for the variables. Not that it would matter if length is a function and OP wanted to use it as a variable.

Link to post
Share on other sites

Okay, this is the problem I see (I added comments)

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include <iostream>
#include <iomanip>

using namespace std;

class rectangle
{
public:
    void setlength(double len);
    void setwidth(double wid);
    double perimeter();
    double area();
    void show();
    double length(); //This is being declared as a function when it should be a variable
    double width();  //Ditto

private:

};
#endif

Also unless there's a need for other parts of the program to access length and width variables, they should be private. And if they're public, there's also no need to have mutator functions setlength and setwidth.

Link to post
Share on other sites

12 hours ago, WubGuy said:

 


#ifndef RECTANGLE_H
#define RECTANGLE_H

#include <iostream>
#include <iomanip>

using namespace std;

//...<snip>

 

 

Do not include namespaces in headers, you'll be polluting the global namespace for any compilation unit that includes your header.

Link to post
Share on other sites

On 25.4.2017 at 0:17 PM, Unimportant said:

Do not include namespaces in headers, you'll be polluting the global namespace for any compilation unit that includes your header.

In fact, this header file in its current state doesn't need to include any header files at all.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×