Returning a class

This is a discussion on Returning a class within the C++ Programming forums, part of the General Programming Boards category; I have a find function to a find a name and then return a class. I will show you parts ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Returning a class

    I have a find function to a find a name and then return a class. I will show you parts i think you will need to help me.

    In main()
    Code:
    list	*wineries = new list();
    	winery	*wPtr;
    
    wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
    wineries->insert(winery("Gallo", "Napa Valley", 200, 25));
    wineries->insert(winery("Cooper Mountain", "Willamette Valley", 100, 47));
    
    
    
    cout << "\n+++ list by rating\n";
    	wineries->displayByRating(cout);
    
    	cout << "\n>>> search for \"Gallo\"\n\n";
    	wPtr = wineries->find("Gallo");                      // Where my problem lies
    	if (wPtr != 0)
    		cout << wPtr;
    	else
    		cout << "not found" << endl;
    
    	cout << "\n>>> search for \"No Such\"\n\n";
    	 wPtr = wineries->find("No Such");
    	if (wPtr != 0)
    		cout << wPtr;
    Code:
    winery * const list::find(const char * const name) const
    {
    	
    node * curr;
    
    winery	*wPtr;
    	
    	//wPtr = NULL;
    	for(curr=headByName; curr; curr=curr->nextByName)
    	{
    		if(strcmp(curr->item.getName(), name) == 0)
    		{
                 wPtr = curr->item;                                           // causing error
    			
    		return wPtr;
    		}
    	}
    Code:
    #ifndef _WINERY_
    #define _WINERY_
    
    #include <ostream>
    
    class winery
    {
    public:
    	winery(const char * const name, const char * const location, const int acres, const int rating);
    	virtual ~winery(void);
    	const char * const getName() const;
    	const char * const getLocation() const;
    	const int getAcres() const;
    	const int getRating() const;
    
    	// display headings for lists of wineries
    	static void displayHeadings(std::ostream& out);
    
    	friend std::ostream& operator<<(std::ostream& out, winery *w);
    
    private:
    	char	*name;
    	char	*location;
    	int		acres;
    	int		rating;
    };
    
    #endif // _WINERY_
    Code:
    #ifndef _LIST_
    #define _LIST_
    
    #include <ostream>
    #include "winery.h"
    
    using namespace std;
    
    class list
    {
    public:
    	list(void);				// constructor
    	virtual ~list(void);	// destructor
    	void displayByName(ostream& out) const;
    	void displayByRating(ostream& out) const;
    	void insert(const winery& winery);
    	winery * const find(const char * const name) const;
    	bool remove(const char * const name);
    
    private:
    	struct node
    	{
    		node(const winery& winery);		// constructor
    		winery item;
    		node * nextByName;
    		node * nextByRating;
    	};
    
    	node * headByName;
    	node * headByRating;
    };
    
    #endif // _LIST_
    Error i get: error C2440: '=' : cannot convert from 'winery' to 'winery *'
    I dont know how else to do this?

    I dont know why the code dont line up better i have it VS set to tab 4
    Thank you
    Last edited by mrsirpoopsalot; 10-03-2009 at 01:11 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by mrsirpoopsalot View Post
    Code:
    winery * const list::find(const char * const name) const
    {
    	
    node * curr;
    
    winery	*wPtr;
    	
    	//wPtr = NULL;
    	for(curr=headByName; curr; curr=curr->nextByName)
    	{
    		if(strcmp(curr->item.getName(), name) == 0)
    		{
                 wPtr = curr->item;  // causing error
    			
    		return wPtr;
    		}
    	}
    Code:
    [...]
    	struct node
    	{
    		node(const winery& winery);		// constructor
    		winery item;
    		node * nextByName;
    		node * nextByRating;
    	};
    [...]
    "winery item" should be "winery * item"

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I dont think so nadroj

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if find returns a real honest-to-goodness winery, then wPtr should most definitely not be a pointer, but a real honest-to-goodness winery, then.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what should it be then? either change "winery" to "winery*" in struct "node", or in function "find" assign "wPtr" the address of "curr->item".

    edit: as tabstop mentions, the last option is to change the type of "wPtr". the point is you have a "winery*" and your trying to assign it the value of a "winery". you need to change one of the two types so that they are the same.
    Last edited by nadroj; 10-03-2009 at 03:17 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Thanks guys.. I couldnt change
    winery item; It threw up alot of errors..

    However, i did what you said and it works..
    wPtr = &curr->item;

    Thank you..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 02:52 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. returning a class
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2005, 07:53 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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