Sort list (numeric/text/float/date) using callback


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Sort list (numeric/text/float/date) using callback

  1. #1
    Join Date
    May 2004
    Posts
    70

    Sort list (numeric/text/float/date) using callback

    i refer to
    http://www.codeguru.com/Cpp/controls...cle.php/c1055/

    this guy had an excellent class, but unforturnately i cant get it to work due to compile errors and problems with the variable "m_arrColType"

    forturnately he added a comment to tell us what is "m_arrColType"
    unforturnately i cannot go to the comment page and was booted to the "sitemap" map


    compile error:

    C:\Program Files\Microsoft Visual Studio\xxxxxpg_control_report.cpp(223) : error C2065: 'm_arrColType' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\xxxxxxpg_control_report.cpp(223) : error C2109: subscript requires array or pointer type
    Error executing cl.exe.

    AClient.exe - 2 error(s), 0 warning(s)

    i tried
    Code:
    void pg_control_report::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    	static int  nSortedCol = -1;
    	static bool bSortAscending = true;
    
    	HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
    
    	if( phdn->iButton == 0 )
    	{
    		// User clicked on header using left mouse button
    		if( phdn->iItem == nSortedCol )
    			bSortAscending = !bSortAscending;
    		else
    			bSortAscending = TRUE;
    
    		nSortedCol = phdn->iItem;
    
    		CSortClass csc(this, nSortedCol);
    
    		long m_arrColType[200];
    
    
    		csc.Sort(bSortAscending, (CSortClass::EDataType)
    			m_arrColType[nSortedCol]);
    	}
    	*pResult = 0;
    }
    but the code doesnt work


    can anyone please tell me what is "m_arrColType" at the ending of the page?



    :wave:

  2. #2
    Join Date
    May 2004
    Posts
    70
    i still have problems
    Code:
    // SortClass.h: interface for the CSortClass class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_SORTCLASS_H__C4FFD42B_9BC4_4084_AA1C_DAA42CDCC697__INCLUDED_)
    #define AFX_SORTCLASS_H__C4FFD42B_9BC4_4084_AA1C_DAA42CDCC697__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class CSortClass
    {
    public:
    	enum EDataType {dtNULL, dtINT, dtSTRING, dtDATETIME, dtDEC};
    
    	CSortClass(CListCtrl * _pWnd, const int _iCol);
    	virtual ~CSortClass();
    	void Sort(bool bAsc, EDataType _dtype);
    
    protected:
    	CListCtrl * pWnd;
    	 EDataType m_arrColType[200];
    
    
    
    	static int CALLBACK Compare(LPARAM lParam1, LPARAM lParam2, LPARAM
    		lParamSort);
    
    	struct CSortItem
    	{
    		CSortItem(const DWORD _dw, const CString &_txt);
    		DWORD dw;
    		CString txt;
    	};
    };
    
    #endif // !defined(AFX_SORTCLASS_H__C4FFD42B_9BC4_4084_AA1C_DAA42CDCC697__INCLUDED_)
    yes over here u see that i inserted it there

    and included it in my derived CListCtrl class named (pg_control_report)

    Code:
    // pg_control_report.cpp : implementation file  
    //
    
    #include "stdafx.h"
    #include "AClient.h"
    #include "pg_control_report.h"
    #include "InPlaceEdit.h"
    #include "SortClass.h"
    
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    but still get the same problem :cry:
    something i missed? :confused:
    please assist

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    what exactly is the error message you're getting?
    Danny Kalev

  4. #4
    Join Date
    May 2004
    Posts
    70
    Quote Originally Posted by Danny
    what exactly is the error message you're getting?

    this

    C:\Program Files\Microsoft Visual Studio\xxxxxpg_control_report.cpp(223) : error C2065: 'm_arrColType' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\xxxxxxpg_control_report.cpp(223) : error C2109: subscript requires array or pointer type
    Error executing cl.exe.

    AClient.exe - 2 error(s), 0 warning(s)

  5. #5
    Join Date
    May 2004
    Posts
    70
    any help please?

  6. #6
    Join Date
    Jan 2005
    Posts
    26

    Lightbulb

    As strange as it may sound for some reason, the the Visual C++ Compiler has a weird tendancy to reject perfectly good code usually complaining abuot undeclaired identifiers in this very fassion. No its not just me making a mistake, because the code always compiles fine in the Borland C++ Compiler. (Anytime visual c++ rejects code that I think should compile, I try it in the borland compiler and invariably, the code compiles fine there), For instance once the following hello world program would not compile because aparently cout was an undeclaired identifier

    #include <iostream>

    using namespace std;

    int main()
    {
    cout << "Hello World" << endl;
    return 0;
    }

    Unfortunatly as I said, the compiler for some reason couldn't figureout what 'cout' was. Bare in mind the above code is verbatim. The worse part was, I was at the time trying to help someone learn c++, (hense the hello world program) and didn't need any problems like this.

    When I tried to complain about this tendancy to the visual c++ compiler teem, I got a whole bunch of responses saying that I had made a mistake somewhere and that the compiler doesn't arbitrarily reject code (Trust me, I didn't believe it at first either and proceeded to spend hours looking at this rather short program before I realized that infact I had not made any mistakes).

    As far as this goes, the code you have is perfectly valid. (Sometimes the visual c++ compiler has issues with declairing variables in the middle of a function rather than declairing them all at the front, but that is usually only an issue when switch case statements are involved and the errors are different). What I did when I had the problem one too many times, is, I reloaded the include dirrectory from the installer disk, and lol and behold, the code that a few seconds ago was bad compiled fine. I don't know, sometimes visual c++ is like that. If it still doesn't work, move the array declairation to the beginning of the function, and if it still doesn't work, find another compiler.

    Ps. For those people who don't believe my claims about the compiler rejecting good code, first of all, I know it sounds funny, if I heard someone else making the same claim, I would have a hard time believing it also, and if you feel you need to 'set me streight" in any way, don't bother because I already recieved the same lecture after telling this story once on the msdn news groups. As far as the strange behavior of the compiler, since getting a fresh copy of the include diffectory seemed to fix things, I have a feeling it might been that something went awry durring the instalation of the IDE, we did have a hard time getting it on the computer.
    Last edited by musicalmike235; 07-05-2005 at 02:07 AM.

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by musicalmike235
    As strange as it may sound for some reason, the the Visual C++ Compiler has a weird tendancy to reject perfectly good code usually complaining abuot undeclaired identifiers in this very fassion. No its not just me making a mistake, because the code always compiles fine in the Borland C++ Compiler. (Anytime visual c++ rejects code that I think should compile, I try it in the borland compiler and invariably, the code compiles fine there), For instance once the following hello world program would not compile because aparently cout was an undeclaired identifier

    #include <iostream>

    using namespace std;

    int main()
    {
    cout << "Hello World" << endl;
    return 0;
    }
    Actually, this code *shouldn't* compile. You need to use the qialified name of cout, i.e., std::cout, or provide a using directive/declarations. Blaming the compiler is fine, as long as the code itself is correct...
    Danny Kalev

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by hanhao
    any help please?
    Look at the following function declaration carefully:

    Code:
    static int CALLBACK Compare(LPARAM lParam1, LPARAM lParam2, LPARAM
    		lParamSort);
    Do you notice something suspicious here?
    Hint: the parameters are supposed to be pointers.

    Theres' even more:
    Code:
    csc.Sort(bSortAscending, (CSortClass::EDataType)
    			m_arrColType[nSortedCol]);
    nSortedCol happens to be a negative constant -1. It's an illegal array index.
    I hate to say this but this code isn't exactly a manifestation of state of the art software engineering. It's old (1998) but still contains too many gaffes and bugs that even with respect to 1998 vintage programming -- aren't acceptable. That said, I'm not suggestint that you throw it out of the window. Instead, think of it an exercise in imrpoving the stale design and broken implementation of a class. I'm serious, such tasks are very useful for lmastering good programming practices.
    Last edited by Danny; 07-05-2005 at 12:30 PM.
    Danny Kalev

  9. #9
    Join Date
    Jan 2005
    Posts
    26
    Actually, this code *shouldn't* compile. You need to use the qialified name of cout, i.e., std::cout, or provide a using directive/declarations. Blaming the compiler is fine, as long as the code itself is correct...
    Actually, it is qualified with std:: in that the using namespace dirrective appears before cout << "Hello World" << endl; is called.

    Either way, its no longer an issue, as I said the problem was fixed by copying a new copy of the include dirrectory from the instalation cd.

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    yeah, you're right. I didn't notice the using directive.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center