Thread: re-declaration

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    re-declaration

    Code:
    header file
      struct node {
        int x;
        node *next;
    
        node(y, node *next) 
          : x(y), next(next) {}
      };
    Code:
    cpp file
    List::node::node(int y, node *next) 
    	:  x(y), next(next){
    
     node *cur=head;
    	
    	if(cur){
    	       while(cur->next!=NULL)
    	           cur=cur->next;	           
    	       cur->next=next;
    	       }
    	else
    	head=next;  
    }
    error
    redefinition

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to read the article linked to in my signature about asking smart questions. At the moment, you have not asked any question at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    sorry if i did not make a question
    what im trying to do is to make a constructor in cpp file for struct node in header file
    and the error is redefinition at the line which i write the struct constructor in cpp file

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, declare but do not define the constructor in the header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    im sorry but im not allowed to change the header file
    comes with this homework and am not allowed to change it
    is there any other way which i can do in cpp file?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, if you are not allowed to change the header file, then you cannot define that constructor in the source file since it has been defined in the header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    can you please give an code example of how to not define the struct constructor in source file ?
    if the define its the problem can you give me an exampe of how to not define the constructor and only code in it?
    edit, oh sorry you mean im not allowed to code the struct constructor right?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cable
    you mean im not allowed to code the struct constructor right?
    Yes. For better or for worse, your node struct's constructor is set to initialise its members, and do no more.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaration
    By Tool in forum C Programming
    Replies: 3
    Last Post: 01-17-2010, 10:18 AM
  2. declaration
    By valthyx in forum C Programming
    Replies: 7
    Last Post: 07-28-2009, 04:11 PM
  3. Odd declaration
    By LowlyIntern in forum C++ Programming
    Replies: 32
    Last Post: 01-16-2009, 08:06 AM
  4. *a++ declaration
    By tyskater in forum C Programming
    Replies: 13
    Last Post: 06-19-2004, 05:20 AM
  5. declaration
    By Clay in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-13-2001, 10:20 AM