What is wrong in this??i am tired!

This is a discussion on What is wrong in this??i am tired! within the C++ Programming forums, part of the General Programming Boards category; Hello, i tried running this program but couldn't after having a look at about 30 mins i still couldn't understand ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    What is wrong in this??i am tired!

    Hello,
    i tried running this program but couldn't after having a look at about 30 mins i still couldn't understand what wrong am i doing. I will leave that upto you now. Below is the code..

    Code:
    
    #include <iostream.h>
    
    
    class linklist
    {
    	struct node
    	{
    		int data;
    		struct node *next;
    	}*first;
    
    	int *d;
    		public:
    			friend void display(linklist);
    
    };
    void display(linklist q)
    {
     struct node *temp=q.first;
     int *t=q.d;
    }
    
    int main(void)
    {
    
     linklist q;
     display(q);
    
     return 0;
    }
    why can't q.first's address be assigned to a pointer and why can q.d's address be assigned to pointer?

    and if i want to assign the node's address to some other pointer via friend function then how can i do??
    Last edited by chottachatri; 03-12-2008 at 06:28 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    probably because they are private

    in the future - show your error message
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    So what??if they are private??i can access all private from member functions or friend functions..

    and the error message i get is :-

    cannot convert linklist::node* to node*

    Now can anybody tell me what wrong am i doing?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    node is nested in linklist, so you should write:
    Code:
    void display(linklist q)
    {
        linklist::node* temp = q.first;
        int* t = q.d;
    }
    Also, <iostream.h> should be <iostream>.
    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

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Thanks Laserlight
    you solved my problem!but still explain me how come it's not recognising the node?please explain me?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    but still explain me how come it's not recognising the node?please explain me?
    As I noted, node is nested in linklist.
    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

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    So if it's nested we have to always use scope resolution??

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    So if it's nested we have to always use scope resolution??
    Yes, unless you are in the given scope.
    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

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok that's fine and why is this code not working??even though i have used scope resolution??

    Code:
    #include <iostream.h>
    
    
    class linklist
    {
       {
    	int *d;
       }
    		public:
    			friend void display(linklist);
    
    };
    void display(linklist q)
    {
     int linklist::*t=q.d;
    }
    
    int main(void)
    {
     linklist q;
     display(q);
    
     return 0;
    }
    i get error declaration terminated incorrectly?and also d is not a member of linklist?but i have declared d in class linklist only?if i have used braces so what?you said if it's nested we have to use scope resolution then why is it not working in this case?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    if i have used braces so what?you said if it's nested we have to use scope resolution then why is it not working in this case?
    You have taken my statement out of context. If you have a nested class, then you qualify its name with the scope resolution operator. If you have a member variable, you access it as usual. What are you trying to do?
    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

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok fine but i m not able to declare only
    whenever i provide braces i get error during compilation that "declaration terminated"

    i am just trying to do as many errors as i could and then solve it. I feel this is the best way to learn programming language..

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    Ok fine but i m not able to declare only
    whenever i provide braces i get error during compilation that "declaration terminated"
    d exists in an inner scope, but you have no way to refer to this scope. I daresay that d is quite useless like this. Just remove the braces and let it live as a normal member variable of linklist.

    i am just trying to do as many errors as i could and then solve it. I feel this is the best way to learn programming language..
    I think you should read a good book first, e.g., Accelerated C++ by Koenig and Moo. If not, you would not be able to reason out the errors, and can only rely on guess and check.
    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

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Types, classes, structs, etc, do not belong to any given instance of any object. It is simply a type part of that blueprint, so you have to type the full scope.
    Member variables, however, are part of the class instances, so you can't use he scope to access them (unless they are static, but that's another lesson).
    I think you got it around your backfoot. You are trying to create a pointer to a class of type linklist. You are not accessing any type inside the class scope, so remove the "::".
    Further, it seems that seem t think that "t" is somehow part of linklist? How do you figure this? The first part of the line is completely wrong. If we remove the dependant name, we get:
    int* t = q.d;
    Which would work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Check this out, perhaps it might help you:
    Code:
    #include <iostream>
    
    class linklist{
    	
    	public:
    		
    	        linklist() : head(0) {};
    	       ~linklist();
    		
    		void push_back(int d);
    		friend void display(linklist& m_list);
    	
    	private:
    	
    		struct node{
    			
    			node(int d) : data(d), next(0) {};
    			
    			int data;
    			node* next;
    		};
    		
    		node* head;
    };
    
    linklist::~linklist(){
    	
    	node *curr, *prev;
    	
    	while(head->next != 0){
    		
    		curr = head;
    		
    		while(curr->next != 0){
    			
    			prev = curr;
    			curr = curr->next;
    		}
    		
    		prev->next = 0;
    		delete curr;
    	}
    	
    	delete head;
    }
    	
    
    void linklist::push_back(int d){
    	
    	if(head != 0){
    		
    		node *curr = head;
    		
                    while(curr->next != 0){
    			
    			curr = curr->next;
    		}
    
    		curr->next = new node(d);
    	}
    	
    	else{
    
    		head = new node(d);
    	}
    }
    
    void display(linklist& m_list){
    	
    	linklist::node *curr = m_list.head;
    	
            while(curr != 0){
    		
    		std::cout << curr->data << "\n";
    		curr = curr->next;
    	}
    }
    
    
    /*  MAIN:
    */
    int main(){
    	
    	linklist m_list;
    	
    	for(int i = 0; i < 10; i++)
    		m_list.push_back(i + 1);
    	
    	display(m_list);
    	
    	return 0;
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by dudeomanodude View Post
    Check this out, perhaps it might help you:
    Code:
    linklist::~linklist(){
    	
    	node *curr, *prev;
    	
    	while(head->next != 0){
    		
    		curr = head;
    		
    		while(curr->next != 0){
    			
    			prev = curr;
    			curr = curr->next;
    		}
    		
    		prev->next = 0;
    		delete curr;
    	}
    	
    	delete head;
    }
    Hmm, that's the worst singly-linked-list destructor I've ever seen - It's O(n*n)!
    I really hope you don't use that in production code, and strongly suggest you learn how to do it in O(n), preferably without recursion too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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