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
    22,258
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

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

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