re-declaration

This is a discussion on re-declaration within the C++ Programming forums, part of the General Programming Boards category; Code: header file struct node { int x; node *next; node(y, node *next) : x(y), next(next) {} }; Code: cpp ...

  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
    22,142
    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.
    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
    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
    22,142
    So, declare but do not define the constructor in the header file.
    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
    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
    22,142
    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.
    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
    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
    22,142
    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.
    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

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, 05: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, 06:20 AM
  5. declaration
    By Clay in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-13-2001, 11:20 AM

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