Thread: more than one function variable?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    38

    more than one function variable?

    Dear,
    It is possible to add more than one function variable in a class(not main class).
    Sorry, i may not make the question clearly.
    Thank you

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    class Foo
    {
    public:
        void bar() {}
        void bar2() {}
    };
    gg

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    38
    Thank you.
    Code:
    private:
    	Cshunt shunt;
    	Ctcircuit tcircuit;
    if i delete one function variable, then problem runs properly.
    Last edited by lwong; 11-16-2003 at 01:48 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    lwong, you might have to elaborate a little.

    e.g. give a bit more relevant code, state compile log, explain how it is not working as expected etc.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    38
    Sorry
    lcircuit2.h
    Code:
    #if !defined(AFX_LCIRCUIT2_H__8CC450C8_55E8_42D6_BD59_33E28683A1A8__INCLUDED_)
    #define AFX_LCIRCUIT2_H__8CC450C8_55E8_42D6_BD59_33E28683A1A8__INCLUDED_
    
    #include "shunt.h"
    #include "tcircuit.h"	// Added by ClassView
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // lcircuit2.h : header file
    //
    
    /////////////////////////////////////////////////////////////////////////////
    // Clcircuit2 dialog
    
    class Clcircuit2 : public CDialog
    {
    // Construction
    public:
    	Clcircuit2(CWnd* pParent = NULL);   // standard constructor
    	
    // Dialog Data
    	//{{AFX_DATA(Clcircuit2)
    	enum { IDD = IDD_lcircuit2 };
    	double	m_rvalue;
    	double	m_rnewvalue;
    	double	m_frequency;
    	//}}AFX_DATA
    
    
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(Clcircuit2)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    
    	// Generated message map functions
    	//{{AFX_MSG(Clcircuit2)
    	virtual void OnOK();
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP();
    private:
    	Cshunt shunt;
    	Ctcircuit tcircuit;
    };
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_LCIRCUIT2_H__8CC450C8_55E8_42D6_BD59_33E28683A1A8__INCLUDED_)
    lcircuit2.cpp
    Code:
    // lcircuit2.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "test.h"
    #include "lcircuit2.h"
    #include "shunt.h"
    #include "tcircuit.h"
    #include "math.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // Clcircuit2 dialog
    
    
    Clcircuit2::Clcircuit2(CWnd* pParent /*=NULL*/)
    	: CDialog(Clcircuit2::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(Clcircuit2)
    	m_rvalue = 0.0;
    	m_rnewvalue = 0.0;
    	m_frequency = 0.0;
    	//}}AFX_DATA_INIT
    }
    
    
    void Clcircuit2::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(Clcircuit2)
    	DDX_Text(pDX, IDC_EDIT1, m_rvalue);
    	DDX_Text(pDX, IDC_EDIT2, m_rnewvalue);
    	DDX_Text(pDX, IDC_EDIT3, m_frequency);
    	//}}AFX_DATA_MAP
    }
    
    
    BEGIN_MESSAGE_MAP(Clcircuit2, CDialog)
    	//{{AFX_MSG_MAP(Clcircuit2)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // Clcircuit2 message handlers
    
    
    void Clcircuit2::OnOK() 
    {
    	// TODO: Add extra validation here
    	double R=0;
    	double newR=0;
    	double frequency=0;
    	double q=0;
    	double pi=3.142857143;
    	double L=0;
    	double C=0;
    	double w=0;
    	UpdateData(1);
    	R=m_rvalue;
    	newR=m_rnewvalue;
    	frequency=m_frequency * 1000000;
    	w=2 * pi * frequency;
    	if (newR>=R)
    	{
    		q=sqrt(newR/R-1);
    		L=q*R/w;
    //		C=L/(power(R,2)+power((w*L),2));
    		tcircuit.m_lvalue=L;
    		tcircuit.m_qvalue=q;
    		tcircuit.DoModal();
    
    	//	tcircuit.m_cvalue=C;
    	}
    	else
    	
    	{ 
    		q=sqrt(R/newR-1);
    	   	shunt.m_value=q;
    		shunt.DoModal();
    	
    	}
    UpdateData(0);
    CDialog::OnOK();
    }
    shunt.h
    Code:
    #if !defined(AFX_SHUNT_H__35B5A7B5_EFB0_4B30_80F5_225057AB7B12__INCLUDED_)
    #define AFX_SHUNT_H__35B5A7B5_EFB0_4B30_80F5_225057AB7B12__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // shunt.h : header file
    //
    
    /////////////////////////////////////////////////////////////////////////////
    // Cshunt dialog
    
    class Cshunt : public CDialog
    {
    // Construction
    public:
    	Cshunt(CWnd* pParent = NULL);   // standard constructor
    
    // Dialog Data
    	//{{AFX_DATA(Cshunt)
    	enum { IDD = IDD_DIALOG3 };
    	double	m_value;
    	//}}AFX_DATA
    
    
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(Cshunt)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    
    	// Generated message map functions
    	//{{AFX_MSG(Cshunt)
    		// NOTE: the ClassWizard will add member functions here
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_SHUNT_H__35B5A7B5_EFB0_4B30_80F5_225057AB7B12__INCLUDED_)
    shunt.cpp

    Code:
    // shunt.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "test.h"
    #include "shunt.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // Cshunt dialog
    
    
    Cshunt::Cshunt(CWnd* pParent /*=NULL*/)
    	: CDialog(Cshunt::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(Cshunt)
    	m_value = 0.0;
    	//}}AFX_DATA_INIT
    }
    
    
    void Cshunt::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(Cshunt)
    	DDX_Text(pDX, IDC_EDIT1, m_value);
    	//}}AFX_DATA_MAP
    }
    
    
    BEGIN_MESSAGE_MAP(Cshunt, CDialog)
    	//{{AFX_MSG_MAP(Cshunt)
    		// NOTE: the ClassWizard will add message map macros here
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // Cshunt message handlers
    Thank you, i don't know whether these codes can help.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What is your error?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    38
    My code is no any error, but cannot run.
    the problem is caused by
    Code:
    private:
    	Cshunt shunt;
    	Ctcircuit tcircuit;
    in the header file "lcircuit1.h"

    if i delete one of function variable, the program can run properly.
    will there be an error if i declare two variable in one class(not main class)?I can declare two variables in main class but i cannot declare more than two. why?
    Thank you.
    Last edited by lwong; 11-16-2003 at 04:04 AM.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    does your code work if u delete Cshunt shunt ....

    anyway u should move object (Cshunt shunt and Ctcircuit tcircuit) declaration into main(), as i see it you are defining 2 "objects" NOT variables in private of class Ctcircuit ....

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Exactly what error are you getting from the complier?

    Undeclared identifier when using the object?
    syntax error: missing ; before shunt (or tcircuit)


    >>private:
    Cshunt shunt;
    Ctcircuit tcircuit;
    <<

    this is can be done.







    Try this;

    lcircuit2.cpp : implementation file

    #include "stdafx.h"
    #include "test.h"
    #include "lcircuit2.h"
    #include "shunt.h"
    #include "tcircuit.h"

    the order may be wrong

    lcircuit2.cpp : implementation file
    #include "stdafx.h"
    #include "test.h"

    //ensure all other headers required by shunt and tcircuit have been included (ie math.h)

    //now include all these objects header as they will be required for the class header
    #include "shunt.h"
    #include "tcircuit.h"

    //last include the class header
    #include "lcircuit2.h"



    or are you including shunt or lcircuit2 in tcircuit? (which you can't do)


    >>anyway u should move object (Cshunt shunt and Ctcircuit tcircuit) declaration into main()

    This is MFC so there is not a main() function.............
    These look like dialogs and so need global scope unless run as modal ( DoModal() ).........
    ect.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    38
    Thank you all,
    i have solved the problem,the code is no problem, after i click rebuild all in build, it can run

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM