public accesor function

This is a discussion on public accesor function within the C++ Programming forums, part of the General Programming Boards category; Hello, I've run the following: Code: #include <iostream> using namespace std; class Rectangle { private: int x, y, w, h; ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    public accesor function

    Hello,

    I've run the following:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Rectangle
    {
    	private:
    	int x, y, w, h;
    	
    	public:
    	Rectangle();
    	int get_width() {return w;}
    	int get_height() {return h;}
    };
    
    class Area
    {
    	private:
    	int area;
    	
    	public:
    	Area();
    	int get_area();
    };
    
    Area::Area()
    {
    	area = 0;
    }
    
    int Area::get_area()
    {
    	area = myRectangle.get_width() * myRectangle.get_height();
    	return area;
    }
    
    Rectangle::Rectangle()
    {
    	x = 0;
    	y = 0;
    	w = 10;
    	h = 20;
    }
    
    int main()
    {
    	Rectangle myRectangle;
    	Area myArea();
    	
    	
    	cout << myArea.get_area() << endl;
    	cout << myRectangle.get_width() << endl;
    
    	return 0;
    }
    I get the following errors:
    Code:
    class5.cpp: In member function ‘int Area::get_area()’:
    class5.cpp:33: error: ‘myRectangle’ was not declared in this scope
    class5.cpp: In function ‘int main()’:
    class5.cpp:51: error: request for member ‘get_area’ in ‘myArea’, which is of non-class type ‘Area ()()’
    How do I access the get_width() and get_height() functions in get_area()? Since they are public in Rectangle, shouldn't I be able to access them in Area? If I comment out the two lines giving me issues, I can access myRectangle.get_width() from main. I don't understand why I can access get_width() in main but not in get_area().

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,327
    Quote Originally Posted by swappo
    Since they are public in Rectangle, shouldn't I be able to access them in Area?
    Yes, if you had a Rectangle to access them with. Unfortunately, you don't, since the Area class does not have any Rectangle related member variable and its get_area() member function does not have any parameters related to Rectangle.
    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
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    The C++ Compiler looks only at local and global variables. MyRectangle is neither for get_area. It's declared in another function, main. You can't access variables declared in a calling function (except passing them as an argument).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    I tried putting an argument in the get_area function.
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Rectangle
    {
    	private:
    	int x, y, w, h;
    	
    	public:
    	Rectangle();
    	int get_width() {return w;}
    	int get_height() {return h;}
    };
    
    class Area
    {
    	private:
    	int area;
    	
    	public:
    	Area();
    	int get_area(Rectangle *myRectangle);
    };
    
    Area::Area()
    {
    	area = 0;
    }
    
    int Area::get_area(Rectangle *myRectangle)
    {
    	area = myRectangle->get_width() * myRectangle->get_height();
    	return area;
    }
    
    Rectangle::Rectangle()
    {
    	x = 0;
    	y = 0;
    	w = 10;
    	h = 20;
    }
    
    int main()
    {
    	Rectangle myRectangle;
    	Area myArea();
    	
    	
    	cout << myArea->get_area(&myRectangle) << endl;
    	cout << myRectangle.get_width() << endl;
    
    	return 0;
    }
    I got the following error.
    Code:
    class5.cpp: In function ‘int main()’:
    class5.cpp:51: error: request for member ‘get_area’ in ‘myArea’, which is of non-class type ‘Area ()()’
    I am not sure whether to pass the class to get_area() or to pass get_width() and get_height().

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Area myArea;
    Otherwise myArea is a function declaration.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Process Memory
    By polydegmon in forum C# Programming
    Replies: 0
    Last Post: 05-26-2009, 07:18 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  4. Stuck with Structs...
    By DanFraser in forum C# Programming
    Replies: 8
    Last Post: 05-03-2007, 09:55 AM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM

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