Class

This is a discussion on Class within the C++ Programming forums, part of the General Programming Boards category; Hi My compilator returns an error in case of my new class, which is in future supposed to operate a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Class

    Hi
    My compilator returns an error in case of my new class, which is in future supposed to operate a calculator. Anyways the class is declared like this:

    Code:
    class Calculator
    {
    protected:
    // Member variables
    	float m_iNum;
    	float m_iX;
    	float m_iSubTotal;
    	float m_iTotal;
    	bool m_bNewTurn;
    public:
    // Constructor ~destructor
    	Calculator();
    	virtual ~Calculator();
    // Public methods
    	void _AddNum(int x);
    	void _Plus();
    	void _Minus();
    	void _Times();
    	void _Devide();
    	float _GetSubTotal() { return m_iSubTotal; };
    	float _GetTotal() { return m_iTotal; };
    	void _SetSubTotal(float m_iNum) { m_iSubTotal = m_iNum; };
    	void _SetTotal(float m_iNum) { m_iTotal = m_iNum; };
    
    	float iTotal;
    };
    The compilator return an error: Error 1 error C2011: 'Calculator' : 'class' type redefinition c:\documents and settings\gordon\desktop\stopwatch\calculator.h 2

    Do you have any ideas what might be a problem or how to solve that.
    Thank you very much

    MS Visual Studio 2005

  2. #2
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Can you post more of your code? It is impossible for me to tell what the problem is without the .cpp file that is using this class.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Your header is missing header guards.

    BTW, it's "compiler", not "compilator".

    Also, why are all your method names prefixed with an underscore? Aside from being unnecessarily unreadable, it's also invalid: all identifiers starting with a underscore followed by another underscore or an uppercase letter are globally reserved for the implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Oh well... guess i didn't know that.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    You have another class called "Calculator" somewhere in your code, or you have included the file twice. You cannot have two classes with the exact same name.

    BTW, I think the correct spelling is "Divide" not "Devide".

    EDIT: too late..

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by CornedBee View Post
    Also, why are all your method names prefixed with an underscore? Aside from being unnecessarily unreadable, it's also invalid: all identifiers starting with a underscore followed by another underscore or an uppercase letter are globally reserved for the implementation.
    How could the implementation put symbols inside his Calculator class?

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by beene View Post
    Can you post more of your code? It is impossible for me to tell what the problem is without the .cpp file that is using this class.
    The code is separated to several files:
    The declaration of the class is:

    Calculator* _pCalc;
    _pCalc = new Calculator();


    (The calculator class initialization stays the same)

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by brewbuck View Post
    How could the implementation put symbols inside his Calculator class?
    It could define a macro. Or the compiler could have an extension keyword by that name.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by C+/- View Post
    You have another class called "Calculator" somewhere in your code, or you have included the file twice. You cannot have two classes with the exact same name.

    BTW, I think the correct spelling is "Divide" not "Devide".

    EDIT: too late..
    Yes I accidently added twice the same header file (in different source files)
    Now as there is just one include a new error appears:

    Error 2 error LNK2019: unresolved external symbol "public: __thiscall Calculator::Calculator(void)" (??0Calculator@@QAE@XZ) referenced in function "int __cdecl GameInitialize(struct HINSTANCE__ *)" (?GameInitialize@@YAHPAUHINSTANCE__@@@Z) Skeleton.obj

    The class calculator code has no change...

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Have you implemented Calculator's constructor? Is the implementation file part of the project?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by CornedBee View Post
    Have you implemented Calculator's constructor? Is the implementation file part of the project?
    Not sure what you mean:

    do you mean
    Calculator* _pCalc;
    and
    _pCalc = new Calculator();

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    What CornedBee means is that because you have a constructor defined in your class:
    Code:
    Calculator();
    you need to implement it somewhere:
    Code:
    Calculator::Calculator() {
        /* ... */
    }
    And you should make sure that whatever file contains this implementation of Calculator() is included in your project and linked along with the rest of your program.

    Also, why are you using floats instead of doubles? Doubles give you more precision. There's really no reason to use floats nowadays, especially if you need precision, as you probably would in a calculator.
    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
    Apr 2007
    Posts
    162
    Oh I see now. So I inserted the implementation, for the record the header code looks like this now:

    Code:
    class Calculator
    {
    private:
    // Member variables
    	float m_iNum;
    	float m_iX;
    	float m_iSubTotal;
    	float m_iTotal;
    	bool m_bNewTurn;
    public:
    // Constructor ~destructor
    	Calculator();
    	virtual ~Calculator();
    // Public methods
    	void _AddNum(int x);
    	void _Plus();
    	void _Minus();
    	void _Times();
    	void _Divide();
    	float _GetSubTotal() { return m_iSubTotal; };
    	float _GetTotal() { return m_iTotal; };
    	void _SetSubTotal(float m_iNum) { m_iSubTotal = m_iNum; };
    	void _SetTotal(float m_iNum) { m_iTotal = m_iNum; };
    
    	float iTotal;
    };
    
    Calculator::Calculator()
    {
    	// Implementation
    }
    I realize placing the implementation into header file is not the best place but for this example it works. Doesnt it?

    But still the same error appears:
    Error 2 error LNK2019: unresolved external symbol "public: virtual __thiscall Calculator::~Calculator(void)" (??1Calculator@@UAE@XZ) referenced in function "public: virtual void * __thiscall Calculator::`scalar deleting destructor'(unsigned int)" (??_GCalculator@@UAEPAXI@Z) Skeleton.obj
    and
    Error 3 fatal error LNK1120: 1 unresolved externals c:\Documents and Settings\Gordon\Desktop\StopWatch\Debug\GameSkelet on.exe 1


    About the type of integers, there is no reason I do not use double ints I can change it but I do not think Id need more than 38 integers in a row in this calc.
    Indeed thx for advice

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> but for this example it works. Doesnt it?
    Maybe, maybe not. But you should still move it. You can either put into a cpp file like calculator.cpp, or you can move it to inside the class declaration.

    >> But still the same error appears:
    Now its complaining about the destructor. Have you implemented any of the functions yet? If not, then you will get these same errors for any functions you use but don't actually define.

    >> About the type of integers, there is no reason I do not use double ints
    I think you are confused about terminology. An integer is a whole number, a floating point number is a number with a decimal or fractional part. The int type only works on integers, but you want a floating point type, so you used float. However, C++ provides a type called double which is probably better than float for you. I'm not sure what the 38 has to do with anything, where did you get that number?

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Ok I will work on that as you said...

    Forget the float or double I will figure it out. I meant that float type can be as long as 38 numbers in a row (1 x 10 to the power of 38) with a sign.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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