Why This doesn't work and the other one works?

This is a discussion on Why This doesn't work and the other one works? within the C++ Programming forums, part of the General Programming Boards category; Ok matsp.. the problem is that it's not compiling! i get a compile time error. Leaving apart the logical errors ...

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok matsp.. the problem is that it's not compiling!

    i get a compile time error. Leaving apart the logical errors like not allocating memory just i want to the syntactical error! in the definition of the 2 functions! why 1 ones and other one even though it's exactly same not working??

    i get a compile time error "Undefined Structure student"

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    These both function declarations mean the same thing. Don't they?then when i call sort(student []); i get a error like undefined structure student
    WHy SO???
    What do you mean by calling that function like that???

    You have declared the same friend function twice, using different notation, but compilers would accept either (you shouldn't use both, though). Care to post updated code, because your code had very many problems related to memory management, any of which would be enough to make the code "not work".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok fine this is the new updated code

    Code:
    #include <iostream.h>
    #include <string.h>
    class student
    {
      char *name,roll[3];
      public:
    	student()
    	{
    
    	}
    	student(char *n,char *r)
    	{
    		name=new char[20];
    		strcpy(name,n);
    		strcpy(roll,r);
    	}
       friend void sort(student []); //Why This Doesn't Work? A compile time error like "undefined structure student"
    
    };
    void sort(student a[])         //Why Not Working??
    {
      int j;
      student temp;
      for(int i=0;i<3;i++)
      {
    	for(j=0;j<2;j++)
    	{
    		if (strcmp(a[j].roll,a[j+1].roll)>0)
    		{
    			temp=a[j];
    			a[j]=a[j+1];
    			a[j+1]=temp;
    		}
    	}
      }
    }
    
    
    int main(void)
    {
     student a[3];
     a[0]=student("abc","1");
     a[1]=student("def","2");
     a[2]=student("hij","3");
    
     sort(a);
    
     return 0;
    }

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The code you have just posted works just fine in g++ - perhaps it's ye olde compiler you are using? - it even runs without crashing - although it produces no output (aside from the "Done" that I added to see that it finishes).

    --
    Mats
    Last edited by matsp; 03-26-2008 at 07:44 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,639
    //Why This Doesn't Work? A compile time error like "undefined structure student"
    What compiler are you using? It looks correct and compiles on MSVC8, at least after I changed the pre-standard <iostream.h> to <iostream> (but it is not needed anyway). You should also change <string.h> to <cstring>, and indent your code more consistently.
    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

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    OR you can get Visual Studio Express and let it automatically indent for you.
    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.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,639
    OR you can get Visual Studio Express and let it automatically indent for you.
    Note that other IDEs and specialised tools can also "beautify" code, but ultimately it is good practice to get into the habit of consistent formatting as you type your code.
    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

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

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