Problem

This is a discussion on Problem within the C++ Programming forums, part of the General Programming Boards category; Say I have an array and I want to sort it alphabetically...how would I do that? Does anyone have the ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    31

    *New* Problem

    Say I have an array and I want to sort it alphabetically...how would I do that? Does anyone have the code?
    Last edited by Aakash Datt; 06-08-2003 at 02:36 PM.

  2. #2
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    you would probably get more replies you have put something other than "problem" in there. Just about everyone posting is posting because of a problem. Be more specific. Sorry I dont know the answer to your problem. Anyway, just a tip for the future

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    11
    Regarding reading from a data file, as far as i know we always use something like the following: (its not c/c++ coding, just algo.)

    while (!EOF){

    read the data from file

    }


    So I don't know why you need variable "i" as a counter to read from file?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    31

    What does this mean?

    Student.obj : error LNK2001: unresolved external symbol "public: void __thiscall Student::Quicksort(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,int)" (?Quicksort@Student@@QAEXPAV?$basic_string@DU?$cha r_t
    raits@D@std@@V?$allocator@D@2@@std@@H@Z)
    Debug/Student.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    31
    Attached is the code...
    Attached Files Attached Files

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The linker error is probably caused by the templates. Here is a link that describes how compilers tend to handle templates, and why you might need both declaration and definition in the same file. Also, why are you using that other Vector class instead of STL vectors?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Why are you declaring your own vector class, and your own quicksort? There are built-in features of the C++ language to do exactly those things. You should use the standard library, not roll your own, because their code is almost without exception going to be safer and more efficient.

    That said, your error is here:

    Declaration of Quicksort:

    Code:
    class Student
    {
    public:
      /* ... */
      void Quicksort(T a[], int n);
      /* ... */
    };
    "Definition" of Quicksort:

    Code:
    template <typename T>
    T Quicksort(T a[], int n)
    This isn't defining what you think it is. Try:

    Code:
    template <typename T>
    void Student::Quicksort(T a[], int n)
    Last edited by Cat; 06-08-2003 at 05:34 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    31
    I changed it, but it still doesn't work...
    Student.obj : error LNK2001: unresolved external symbol "public: void __thiscall Student::Quicksort(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,int)" (?Quicksort@Student@@QAEXQAV?$basic_string@DU?$
    char_traits@D@std@@V?$allocator@D@2@@std@@H@Z)
    Debug/Student.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Most likely a templates problem. Try putting the declarations and definitions of templated functions in the same file.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Zach L.
    Most likely a templates problem. Try putting the declarations and definitions of templated functions in the same file.
    Good call, this is an excellent point. Declare any template functions inline (that is, include their full body within the header).

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    31
    Can someone do it for me...cuz I can't...

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, why are you even trying to make your own vectors and sort algorithms in the first place?

    Why not just do something like:

    Code:
    #include <vector>
    #include <algorithm>
    
    /*...*/
    
    void Demo() {
      std::vector<Student> collection;
      collection.reserve(25); // Reserve at least enough space for 25 students
      for (int i = 0; i < 25; ++i){
        collection.push_back(Student());  // Add a new student
        collection[i].SetInfo(...);  // and set its info
      }
      std::sort(collection.begin(),collection.end());
    }
    
    /* Note you need to provide "operator<" for class Student; this
    will be needed to sort. No other operators are needed.*/
    I haven't tried to compile it, so the code may be suspect, but it should be close. This code uses the C++ standard ways of doing these things, so it should work better than a homegrown solution.
    Last edited by Cat; 06-08-2003 at 09:41 PM.

  13. #13
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    BTW, here is a quick piece of code I threw together. It does less than what you want, but it illustrates the concept:

    Code:
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <iostream>
    
    class Student{
    public:
        std::string lastName;
        std::string firstName;
        double GPA;
        bool operator < (const Student &)const;
        Student(std::string l = "",std::string f = "", double g = 0);
    };
    
    Student::Student(std::string l ,std::string f, double g): 
        lastName(l), firstName(f),GPA(g){}
    
    bool Student::operator <(const Student & s2)const{
        if (lastName < s2.lastName) return true;
        if (s2.lastName < lastName) return false;
        return firstName < s2.firstName;
    }  
    
    /* Note we use ONLY string::operator <; in general we 
    wouldn't know for sure how any other operators worked in relation to each other.
    For example, it is legal for a < b to return false, b < a to return false,
    and a == b to return false as well, when dealing with class types.
    Indeed, strict weak ordering doesn't require that 
    equivalence, !(a < b || b < a), is the same as equality (a == b).
    
    Note also that we only need a < criteria to sort; the sort
    algorithm only uses operator< for the same reason we did.*/
    
    
    int main(int argc, char* argv[])
    {
    
        std::vector<Student> collection;
        collection.reserve(6);  // not necessary but improves performance
        collection.push_back(Student("Smith","John", 3.7));
        collection.push_back(Student("Derkins","Suzie", 4.0));
        collection.push_back(Student("Brown","Charlie", 3.5));
        collection.push_back(Student("Wagner","Richard", 3.9));
        collection.push_back(Student("Derkins","Jamie", 2.9));
        collection.push_back(Student("smith","Jack", 4.0));
    
        std::sort(collection.begin(),collection.end());
        
        for (int i = 0; i < 6; i++){ // could use iterators, but why not show off random access?
            std::cout << collection[i].lastName << ", ";
            std::cout << collection[i].firstName << ".  GPA: ";
            std::cout << collection[i].GPA << std::endl;
        }
        return 0;
    }
    This produces the following output:
    Brown, Charlie. GPA: 3.5
    Derkins, Jamie. GPA: 2.9
    Derkins, Suzie. GPA: 4
    Smith, John. GPA: 3.7
    Wagner, Richard. GPA: 3.9
    smith, Jack. GPA: 4

    Note it sorts strings case-sensitively by default.
    Last edited by Cat; 06-08-2003 at 10:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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