Static variables and functions problem in a class

This is a discussion on Static variables and functions problem in a class within the C++ Programming forums, part of the General Programming Boards category; Hi everybdoy, I've read a few tutorials on static variables and functions, including the one on this site, and I ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Static variables and functions problem in a class

    Hi everybdoy,

    I've read a few tutorials on static variables and functions, including the one on this site, and I think it makes sense to me. But I keep running into a memory problem. The class I use is this:
    Code:
    class InBAF
    {
    private:
    	static char location;
    	static CString server;
    	static CString file;
    
    public:
    	static void SetLocation(char newloc)
    	{
    		location = newloc;
    	}
    
    	static void SetServerName(char location)
    	{
    		switch (location)
    		{
    		case 'T':	
    				server = "ftp.ca";
    			break;
    
    		case 'M':
    				server = "ftp.ca"; 
    			break;
    		}
    	}
    
    	static void SetFileName(CString name)
    	{
    		file = name;
    	}
    
    	static CString GetServerName()
    	{
    		return server;
    	}
    
    	static char GetLocation()
    	{
    		return location;
    	}
    
    	static CString GetFname()
    	{
    		return file;
    	}
    };
    char InBAF::location = 'N';
    CString InBAF::server;
    CString InBAF::file;
    from previous postts I think I've fixed most of the CString problems in the code. Now when I use one of the functions here:
    Code:
                CListBox* pListSub = (CListBox*)GetDlgItem(IDLB_SUBDIR);
    
    	CString name;
    	pListSub->GetText((pListSub->GetCurSel()), name);
    
    	FILE* out = fopen("h:\\gui\\selsub.txt", "w");
    	fprintf(out, ">%s<\n", name);
    
    	InBAF::SetFileName(name);
    Aside from the Visual part I get an error that memory could not be read and the file contain nothing, not even the > <. However, if I say display the contents in a text box control it displays the contents of name. and I can't step through with he debugger because there is an access violation problem with another function i nthe program that I haven't resolved yet, and don't know what causes it.
    If someone could take a look at the class for any problems that I don't know about please. Thanks in advance.
    Everything is relative...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    FILE* out = fopen("h:\\gui\\selsub.txt", "w");
    You know that is C, right? In C++ you're supposed to use ?fstream.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    You know that is C, right?
    You know that the better part of C is also C++, right?
    In C++ you're supposed to use ?fstream.
    Not really, it's just encouraged. There's nothing wrong with using C style I/O if it works for you.
    Just because I don't care doesn't mean I don't understand.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Yes I know. I'm just used to using and haven't switched over. and I actually noticed that I haven't closed the file that's why it was empty. It was stupidity mistake.
    and I even tried taking out the SetFileName() out of the class an djust usifn a global variable to hold the value of the filename. But when I try to write file = name I get a memory read error.
    I have no clue what is going on here, and I'm running out of ideas of how to fix it. Any constructive input is very welcom.
    Everything is relative...

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You know that the better part of C is also C++, right?
    Yeah, nearly all of it . . . except:
    Code:
    void class() {}

    I can't step through with he debugger because there is an access violation problem with another function i nthe program that I haven't resolved yet
    Maybe your problem is there, then. Post the code that might be suspect.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Here's the function that I get stuck on. Right at the closign brace:
    Code:
    	FileBrowse::ResetFpath("recording:[000000]");
    
    	int radioState =0; 
    	char location = 'N';
    	char buffer[100];
    	FileBrowse ConnectFile;
    	CString localfile;
    
    	CButton* pRadio = (CButton*)GetDlgItem(IDR_TO); 
    	radioState = pRadio->GetCheck();
    
    	if(radioState)
    		location = 'T';
    
    	radioState = 0;
    
    	pRadio = (CButton*)GetDlgItem(IDR_MO); 
    	radioState = pRadio->GetCheck();
    
    	if(radioState)
    		location = 'M';
    
    	radioState = 0;
    
    	pRadio = (CButton*)GetDlgItem(IDR_LO); 
    	radioState = pRadio->GetCheck();
    
    	if(radioState)
    		location = 'L';
    	
    	if(location == 'N')
    	{
    		wsprintf(buffer, "Please select a location of the .baf file first."); 
    		MessageBox(buffer, NULL, MB_OK | MB_ICONEXCLAMATION);
    		return;
    	}
    
    	CInternetSession isession;
    
    	CFtpConnection* pConnect;
    	CString Server;
    
    	if(location == 'L')
    	{
    		CMyFileDialog  FileDlg(TRUE,0,0,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,0,this);
    	
    		if(FileDlg.DoModal() == IDOK)
    		{
    			localfile = FileDlg.GetPathName();
    			CEdit* pEditSelFile = (CEdit*)GetDlgItem(IDE_FILEPATH);
    	        pEditSelFile->SetWindowText(localfile);
    		}
    
    		InBAF::SetLocation(location);
    		return;
    	}
    	else
    	{
    		InBAF::SetLocation(location);
    		InBAF::SetServerName(location);
    		Server = InBAF::GetServerName();
    	}
    
    	pConnect = isession.GetFtpConnection(_T(Server), "USER");
    		
    	//CMyConnection::InitConnection(pConnect);
    	
    	pConnect->SetCurrentDirectory("recording");
    	
    			FILE *out = fopen("H:\\GUI\\testInet-main.txt", "w");
    
    	CFtpFileFind finder(pConnect);
    
    	CListBox* pListMain = (CListBox*)GetDlgItem(IDLB_MAINDIR); 
    	CListBox* pListSub = (CListBox*)GetDlgItem(IDLB_SUBDIR);
    	
    	// Find  the first file 
    	BOOL bWorking = finder.FindFile(NULL);
    	CString rfname, nfname;
    	
    	// Go through the rest of the directory and designate files.
    	while (bWorking)
        {
    		bWorking = finder.FindNextFile();
    
    		rfname = finder.GetFilePath();
    
    		if (strstr(rfname, ".BAF"))
    		{
    			nfname = ConnectFile.MakeName(rfname);
    			pListSub->AddString(nfname);
    			fprintf(out, "\t\t\tSub: %s\n", nfname);
    		}
    		else if(strstr(rfname, ".DIR"))
    		{
    			nfname = ConnectFile.MakeName(rfname);
    			pListMain->AddString(nfname);
    			fprintf(out, "\t\t\tMain: %s\n", nfname);
    		}
    		rfname.Empty();
    		nfname.Empty();
    
    	}
    
    	// Display current directory in edit box
    
    	CEdit* pEditSelFile = (CEdit*)GetDlgItem(IDE_FILEPATH);
    	CString filepath = FileBrowse::GetFPath();
    	pEditSelFile->SetWindowText(filepath);
    
    	
    	// if the connection is open, close it
    	if (pConnect != NULL)
    		pConnect->Close();
    	delete pConnect;
    
    	fclose(out);
    }
    I know it's a visual app, but I tested that part of the code and it should be fine. The other class is this ,FileBrowse:
    Code:
    class FileBrowse
    {
    private:
    	static CString fpath;
    
    public:
    	//CString rfname , nfname ;
    
    	void SetFPath(CString rfname)
    	{
    		char temp[8];
    		int index, indexn, i, k=0;
    
    		index = rfname.Find('/');
    
    		i=(index+1);
    
    		if(strstr(rfname,".DIR"))
    			indexn = rfname.Find(".DIR");
    
    		else if(strstr(rfname,".LSH"))
    			indexn = rfname.Find(".LSH");
    
    		while(i<indexn && k<8)
    		{
    			temp[k] = rfname[i];
    			k++; i++;
    			
    		}
    
    		temp[k] = '\0';
    
    		indexn = fpath.ReverseFind(']');
    
    		fpath.SetAt(indexn, '.');
    
    		fpath= fpath + temp +']';
    	}
    
    	void PrintFpath(FILE* out)
    	{
    		fprintf(out, "\t\t>%s<\n", fpath);
    	}
    
    	static CString GetFPath()
    	{
    		return fpath;
    	}
    	CString MakeName(CString rfname)
    	{
    		char temp[14] = {0};
    		int index, indexn, i, k=0;
    		CString nfname;
    
    		index = rfname.Find('/');
    		
    		i=(index+1);
    
    		if(strstr(rfname,".DIR"))
    		{
    			indexn = rfname.Find(".DIR");
    			while(i<(indexn) && k<14)
    			{
    				temp[k] = rfname[i];
    				k++; i++;
    				
    			}
    			temp[k] = '\0';
    		}
    
    		else 
    		{
    			indexn = rfname.Find(".BAF");
    
    			while(i<(indexn+4) && k<14)
    			{
    				temp[k] = rfname[i];
    				k++; i++;
    				
    			}
    			temp[k] = '\0';
    
    			return nfname = temp;
    			
    		}
    		
    		nfname = fpath;
    
    		indexn = nfname.ReverseFind(']');
    
    		nfname.SetAt(indexn, '.');
    
    		nfname = nfname + temp + ']';
    		
    		return nfname;
    	}
    
    	static ResetFpath(CString newpath)
    	{
    		fpath = newpath;
    	}
    };
    CString FileBrowse::fpath = "]";
    Sorry it's so lengthy.
    Everything is relative...

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    i noticed that one of non-static methods uses a static variable. Is that allowed?
    I creat an instance of that class (ConnectFile) and then use ConnectFile.Makename() and that uses a static variable fro mthat class. Is that legal?

    EDIT: i also tried runnign debuggin that function without using the class and the access violation still occures.
    Last edited by earth_angel; 08-30-2005 at 12:32 PM.
    Everything is relative...

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    i noticed that one of non-static methods uses a static variable. Is that allowed?
    I think so. But vise-versa isn't (a static method accessing a non-static variable).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Put tons of couts in. Maybe it doesn't get past the while (bWorking) loop.

    Then again,
    Right at the closign brace
    Maybe the fclose() fails? Maybe it doesn't even open? Do you really have a drive H?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Actually, your destructor probably does something it shouldn't. Like delete something twice.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I never wrote a destructor. I'll add one to both the classes. Should it be something like this:
    Code:
    ~FileBrowse()
    	{
    
    	}
    but what should it do? I don't ahve any memory allocated to be cleaned up?
    Everything is relative...

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Yeah, I noticed you don't have a destructor. Maybe you have another destructor elsewhere that could cause a problem.

    If you don't provide a destructor, the compiler puts a default one in for you:
    Code:
    ~classname() {}
    And there's a default constructor, too, which also does nothing.]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    If I want to specify a destructor explicitly so I know exactly what it does, for the two classes I have what can it do?
    Everything is relative...

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The compiler generated destructor (or an empty destructor that you write) will do nothing for the two classes above (InBAF and FileBrowse). That is because they have no non-static data to destruct (unless I missed something).

    When you say that it has the problem on the closing brace, do you mean that is where the arrow points to when you break into the debugger, or do you mean you step through the code and that is where the failure occurs? If you put a breakpoint at the fclose line (F9) and then step over it (F10) does it crash there? If not, if you step over the closing brace (F10 again) does it crash there?

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I noticed a couple of things with your FileBrowse class.
    Code:
    >		char temp[8];
    >.
    >.
    >		temp[k] = '\0';
    If k happens to be 8 at this point, then you're going to be writing the string terminator in temp[8], which doesn't exist, as the temp array has indices from 0-7.

    Code:
    >		char temp[14] = {0};
    >.
    >.
    >			temp[k] = '\0';
    Same thing here, indices from 0-13.

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

Similar Threads

  1. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  2. Functions in class and class variables.
    By Karakus in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2006, 02:29 AM
  3. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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