Say I have an array and I want to sort it alphabetically...how would I do that? Does anyone have the code?
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 01:36 PM.
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
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?
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.
Attached is the code...
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.
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:
"Definition" of Quicksort:Code:class Student { public: /* ... */ void Quicksort(T a[], int n); /* ... */ };
This isn't defining what you think it is. Try:Code:template <typename T> T Quicksort(T a[], int n)
Code:template <typename T> void Student::Quicksort(T a[], int n)
Last edited by Cat; 06-08-2003 at 04:34 PM.
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.
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.
Good call, this is an excellent point. Declare any template functions inline (that is, include their full body within the header).Originally posted by Zach L.
Most likely a templates problem. Try putting the declarations and definitions of templated functions in the same file.
Can someone do it for me...cuz I can't...
Well, why are you even trying to make your own vectors and sort algorithms in the first place?
Why not just do something like:
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.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.*/
Last edited by Cat; 06-08-2003 at 08:41 PM.
BTW, here is a quick piece of code I threw together. It does less than what you want, but it illustrates the concept:
This produces the following output: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; }
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 09:14 PM.