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

  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
    28,413
    //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.
    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

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    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