Thread: ADT

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

    ADT

    This is a homework assignment. I say this because i dont want someone to do it for me. However, i want to learn and at this point i am not learning anything. This is our first assignment in data structures.

    Any help in easy to understand language would be great..
    we were given this skeleton program to finish it.
    please forgive me if the formatting is bad. Its how it paste in here.


    Code:
    int main()
    {
    char mytry;
    
    list	*wineries = new list();
    winery	*wPtr;
    cout << "\nCS260 - Lab1 - " << endl << endl;
    
    	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));
    	wineries->insert(winery("Duck Pond Cellars", "Willamette Valley", 845, 70));
    Code:
    #ifndef _LIST_
    #define _LIST_
    
    #include <ostream>
    #include "winery.h"
    
    using namespace std;
    //const int SIZE = 100;
    
    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_
    in the insert function it wont let me create memory for a link list
    node * newNode = new node;

    i get this error:
    error C2512: 'list::node' : no appropriate default constructor available

    so i make a class function:
    Code:
    list::node::node(const winery &winery)
    {
    
    item = winery;
    nextByName = NULL;
    nextByRating = NULL;
    
    }
    and i still get the same error, however i get another one..
    'winery' : no appropriate default constructor available

    Code:
    #include "list.h"
    #include "winery.h"
    
    
    
    list::list()
    {
    
    int acres = 0;
    int rating = 0;
    
    }
    
    list::~list()
    {
    	
    }
    
    void list::displayByName(ostream& out) const
    {
       
    }
    
    void list::displayByRating(ostream& out) const
    {
    	// your code here
    }
    
    void list::insert(const winery& winery)
    {
    
    //node * newNode = new node;
    
    node * prev = NULL;
    node * curr = headByName; 
    
    node *newNode = new node(winery);
    
    
    
    	//traverse to find the position to insert
    	//while (curr!=NULL && curr->item < winery)
    	//{
    	//	prev = curr;
    	//	curr = curr->next;
    	//}
    
    	//the data already exists
    	//if(curr && curr->item == winery)
    	//	return false;
    	//insert the data here
    //	else
    	//{
    		//create new node to contain the data
    	  // node * newNode = new node;
    	 //  newNode->item = winery;
    	 //  newNode->nextByName = NULL;
    
             //  link the newNode into the linked list
    	//   newNode->nextByName = curr;
    	//if(prev == NULL)
            //   headByName = newNode;
          // else
           //    prev->nextByName = newNode;
    		
    		
    }
    
    
    
    }
    
    winery * const list::find(const char * const name) const
    {
    	// your code here, return the appropriate value
    	return 0;
    }
    
    bool list::remove (const char * const name)
    {
    	// your code here, return the appropriate value
    	return false;
    }

    Code:
    #include "winery.h"
    
    using namespace std;
    
    
    
    
    
    winery::winery(const char * const name, const char * const location, const int acres, const int rating)
      : name( new char[strlen(name)+1] ), location( new char[strlen(location)+1] ),acres(0),rating(0)
    {
    strcpy_s( this->name, 200, name );
    strcpy_s( this->location, 200, location );
    this->acres = acres;
    this->rating = rating;
    
    }
    
    winery::~winery()
    {
    
    if(name)
    		delete[] name;
    	if(location)
    		delete[] location;
    
    }
    
    const char * const winery::getName() const
    {
    	//strcpy(name, this->name);
    	return name;
    }
    
    const char * const winery::getLocation() const
    {
    	// your code here, return the appropriate value
    	return location;
    }
    
    const int winery::getAcres() const
    {
    	return acres;
    }
    
    const int winery::getRating() const
    {
    	return rating;
    }
    
    void winery::displayHeadings(ostream& out)
    {
    	// your code here
    }
    
    ostream& operator<<(ostream& out, winery *w)
    {
    	// your code here
    	return out;
    }
    I hope how i laid this out its not too confusing..
    Last edited by CornedBee; 09-27-2009 at 02:30 PM.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    The constructor for winery, node, or both is taking in values which don't match their declarations and definitions.

    This example should have the same error message:

    Code:
    class Test
    {
         int x;
    };
    
    int main()
    {
         Test test(0);
         return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    can you explain a little further, Im not seeing what you are.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You are trying to create a node using a constructor that does not take an argument:
    Code:
    node * newNode = new node;
    the only constructor you've provided is one which takes a winery object:
    Code:
    node(const winery& winery);		// constructor
    Get it?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Using a constructor to build a link list is very new to me and we are not covering it in class.
    How would i use the winery object to create a link list then? I guess im still a little confused =(

    is this better?
    Code:
    node* newNode = new node(winery);
    I thought constructors were just to initialize? Im not sure how the constructor and link list tie in together
    Last edited by mrsirpoopsalot; 09-26-2009 at 03:39 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are not using a constructor to build a linked list, so it's going to remain new to you (especially since such a thing doesn't really make sense). You are using the insert method of a list object to build a list. But you do need to build the nodes, and in order to do that your node requires a winery -- look at where rags_to_riches pointed out your node constructor. So whenever you call a new node, you need to pass the winery in. You are aware you can do something like this, right:
    Code:
    int *foo;
    foo = new int(5);
    where we call the int constructor with a parameter?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    tabstop, your talking about doing this right?

    Code:
    node* newNode = new node(winery);
    what do i need to do in the node constructor then?
    God im confused.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mrsirpoopsalot View Post
    tabstop, your talking about doing this right?

    Code:
    node* newNode = new node(winery);
    what do i need to do in the node constructor then?
    God im confused.
    Why should you need to do anything more than what's already there? The point being, you had to give the winery to the constructor when you call it so that it could use it.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I am still getting this error:
    error C2512: 'winery' : no appropriate default constructor available

    in reference to
    Code:
    list::node::node(const winery &winery)
    {
    
    //item = winery;
    //nextByName = NULL;
    //nextByRating = NULL;
    
    
    }
    Code:
    private:
    	struct node
    	{
    		node(const winery& winery);		// constructor
    		winery item;
    		node * nextByName;
    		node * nextByRating;
    	};
    
    	node * headByName;
    	node * headByRating;
    };

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Same song, second verse:
    Code:
    winery item;
    Your constructor for node is going to have to construct this object, and there are no default constructors. At this point it's getting annoying trying to remember which winery is the type and which winery is the variable, which is why you shouldn't do that. Consequently your node constructor should call the winery constructor, something like this:
    Code:
    node(const winery &incoming) : item(incoming)
    {
        /* set some pointers
    }

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    Quote Originally Posted by mrsirpoopsalot View Post
    I am still getting this error:
    error C2512: 'winery' : no appropriate default constructor available
    Let's go over, again, exactly what your problem is. I didn't point out exactly where your problem is in my previous post, but others have told you pretty much exactly where it is. Let's go over the concept of a constructor and a function, and then you'll see where you went wrong.

    You are no doubt aware of how a function works. You declare it, define it (possibly in the same place), and can pass values to it, based on how it is declared. A constructor is merely a special function. Just like regular functions, constructors can be overloaded. In your case, you need a second constructor, one that takes no arguments. Why? Well let's take at these lines:

    Code:
    node * nextByName;
    node * nextByRating;
    Let's then take a look at this line:

    Code:
    node(const winery& winery);		// constructor
    I hope you notice the type mismatch between the constructor and the nodes. Your constructor basically says that in order to create a node, you must pass it a winery object. The two nodes within your function are not being passed a winery object, and so cannot be built. That is unless you declare a second constructor, like this:

    Code:
    node();		// constructor

  12. #12
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Thank you for all your help! sorry i haven't seen what you guys have been talking about.
    I just didnt see why the constructor would need the winery object.

    The bad part is we are not suppose to change some of the files.
    and i would assume thats where the default node constructor would go
    Code:
    // make NO CHANGES to this file
    
    #ifndef _LIST_
    #define _LIST_
    
    #include <ostream>
    #include "winery.h"
    
    using namespace std;
    //const int SIZE = 100;
    
    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_

    What i did come up with was this.. However, i am not sure if it is right.
    I get no compile errors..
    Code:
    list::node::node(const winery &winery): item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating())
    
    {
    so, obviously i still cant create a node. Again, i apologize if i am not getting this fast enough. Please be patient with me..

    The teacher is not teaching us why we have to do it this way or even how to do it, i am basically on me own. Thank you guys for helping me.

    So i should be able to do this then?
    node* newNode = new node(winery); // crashes
    but what do i have to do within the constructor?

    Sorry if i havent got what you guys are saying!!!!!!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It wouldn't surprise me that your instructor hasn't mentioned how to make exactly this program work; I wouldn't have either. You have the information about what constructors are, how function calls work, what's needed to construct an object in C++, etc. You should then apply that information to the current situation. None of us here are reading off a sheet titled "How the Winery Problem Works"; we're taking the information at hand and our knowledge of the language and using the tools at our disposal to solve a problem.

    You need to sit back, and look at your problem. How of much what you pasted in here was given and how much of it was yours I'm not sure. But for each class you have to write, you need to ask yourself: what members are in this class; how am I going to get data into these members, and what can I do with these members once they're initialized.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It might also help to first approach the problem "on your own terms", that is, write a program that does exactly what this assigment calls for, but using whatever method you're comfortable with. Once you have it working properly, compare the two and slowly build in the functionality into the required format, little by little.

    Along the way, it might be a good idea to get better aquainted with the language by reading as many books/tutorials as possible.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Thanks guys.I guess i just cant think in terms of ADT maybe i just need to rethink a different major. Sorry to waste your guys time..
    I have worked a week on this project and the sad thing is almost everyone in the class is having the same problems i am having.
    Last edited by mrsirpoopsalot; 09-26-2009 at 11:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  2. Understanding ADT Stack
    By smitsky in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2004, 10:23 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. Problem with My Sets ADT
    By jawwadalam in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 06:36 AM
  5. ADT Initialization/Passing of Array from Friended ADT
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2002, 12:45 AM