A newbie question...i think..

This is a discussion on A newbie question...i think.. within the C++ Programming forums, part of the General Programming Boards category; i have a very simple program and i'm running into a "error C2228: left of '.calcArea' must have class/struct/union type" ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    A newbie question...i think..

    i have a very simple program and i'm running into a

    "error C2228: left of '.calcArea' must have class/struct/union type" error.

    i've tried messing with different naming conventions and such...but here's my code:

    Code:
    headerfile:   2dArea.h
    
    class square
    {
    	protected:
    		int length;
    		int height;
    
    	public:
    	
    		SquareCon ();
    		
    		void calcArea (int, int);
    		void setParams (int, int);
    
    };
    
    //class cube: public square
    ///{
    
    //	private:
    	//	int depth;
    
    //	public:
    	//	CubeConstructor ();
    	//	CubeConstructor (int, int, int);
    	//	int calcVolume (int, int, int);
    	
    
    //};
    
    
    2dArea.cpp file:
    
    #include <iostream.h>
    #include "2dArea.h"
    
    
    square::SquareCon ()
    {
    	length = 0;
    	height = 0;
    };
    
    void square::setParams (int len1, int ht1)
    {
    	length = len1;
    	height = ht1;
    };
    
    void square::calcArea (int len1, int ht1)
    {
    	(len1*ht1);
    };
    
    
    main.cpp file:
    
    #include <iostream.h>
    #include "2dArea.h"
    
    
    
    
    extern void main ()
    {
    
    	square SquareCon();
    	int len1;
    	int ht1;
    
    
    
    	cout << "Please enter the length of your square: " << endl;
    	cin >> len1;
    	cout << "Please enter the height of your square: " << endl;
    	cin >> ht1;
    
    	cout << "The area of your square is: "<< SquareCon.calcArea << endl;
    
    
    }
    that last cout line is where i get the error.

    any help would do wonders!

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,801
    This declares a function named SquareCon that takes no arguments and which returns a square object:
    Code:
    square SquareCon();
    What you wanted to write was:
    Code:
    square SquareCon;
    By the way, your calcArea() member function is not actually doing what you probably want it to do. Perhaps it should have an int return type.
    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
    Oct 2008
    Posts
    1,262
    You'll have to change:
    Code:
    	square SquareCon();
    to:
    Code:
    	square SquareCon;
    As the former is assumed to be a function declaration, not a variable declaration. As long as you don't pass anything to the constructor, you shouldn't use the parenthesises.


    Also, you'll need "()" after a function call, here:
    Code:
    	cout << "The area of your square is: "<< SquareCon.calcArea << endl;

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    yes thanks! its kind of weird how minutes after you post something, you can figure it out :-)

    i did what you said and got my things to work. And now i can continue :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21