Thread: Classes: Getting Error Message

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Classes: Getting Error Message

    Hey, all! I am in my first programming class and have done pretty well thus far. I am running into error messages and cannot figure out where I went wrong - please help if you can. Thanks in advance.

    This is the program I have developed:
    Code:
    //Program that defines a class called Plot that contains private members.
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class Plot
    
    
    {
    public:
    	Plot(double,double);
    	void setlength (double l);
    	void setwidth (double w);
    double getlength();
    double getwidth();
    float area();
    float perimeter();
    private:
    	double length;
    	double width;
    };
    
    	Plot::Plot(double,double)
    {
    	length = l;
    	width = w;
    }
    
    void Plot::setlength(double l)
    {
    	length = (l >= 0.0 && l <= 7.0)?l = 0;
    }
    void Plot::setwidth(double w)
    {
    	width = (w >= 0.0 && w <= 9.0)?w = 0;
    }
    float Plot::area()
    {
    	return (l * w);
    }
    float Plot::perimeter()
    {
    	return (2*(l + w));
    }
    int main()
    {
    	Plot rect(7,9);
    	cout<<"The area of the retangle is "<<rect.area()<<endl;
    	cout<<"The perimeter of the rectangle is "<<rect.perimeter()<<endl;
    	return 0;
    }
    Here are the error messaged I am getting:
    Code:
    error C2065: 'l' : undeclared identifier
    error C2065: 'w' : undeclared identifier
    error C2143: syntax error : missing ',' before ';'
    error C2143: syntax error : missing ',' before ';'
    Where the heck would I be missing commas? I have tried to move what I though was the identifier around, but it just causes more chaos. Anything to help is appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    float Plot::area()
    {
    	return (l * w);
    }
    float Plot::perimeter()
    {
    	return (2*(l + w));
    }
    What is l and w? Perhaps you wanted to use length and width instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    what is with setwidth and setlength are u trying to do an if statement there cause thats wrong. and what are u oing woth the functions getwidth and getlength they are doing nothing

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    for the area and perimeter you need to feed it l and w inorder to calculate, also how bout a default constructor, you have an overloaded one but no default. also for the overloaded you need what variable the double is, it dont know. there are many things wrong with your code but start with what i said and go from there i got it working for me but after i murdered ur code though

  5. #5
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    void Plot::setlength(double l)
    {
            length = (l >= 0.0 && l <= 7.0)? l = 0;
    }
    Is it just me or the syntax is all wrong here? i believe it should be
    Code:
    void Plot::setlength(double l)
    {
            length = (l >= 0.0 && l <= 7.0)? l : 0;
    }
    Notice the ':' i believe that can be causing the missing commas error since all the punctuation is being evaluated incorrectly from this point on. Notice this is also happening in Plot::setwidth() method.

    Cheers

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    LinuxCoder - you are awesome! I made the change as suggested and it worked. Intead of using l and w, I switched to "length" and "width". I figured extra typing was worth making the program run!

    Thanks again!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why are you mixing float and double? Just use double everywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM