Thread: Assertion: How to initialize a class object and use it

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Assertion: How to initialize a class object and use it

    Hi all,

    Am trying to figure out how to use assertions and have just installed Visual Assertion.

    Have two projects: Calculator and Test
    Code:
    #include "Calculator.h"
    
    
    Calculator::Calculator(void)
    {
    }
    
    int Calculator::add(int a, int b)
    {
    	int result = a + b;
    	return result;
    }
    
    int main(void)
    {
    	Calculator cal;
    	int result = cal.add(3, 3);
    	return result;
    }
    
    Calculator::~Calculator(void)
    {
    }
    Code:
    #include "stdafx.h"
    #include <cfixcc.h>
    #include "C:\Users\Jannick\Documents\Visual Studio 2010\Projects\Calculator\Calculator\Calculator.h"
    
    class ExampleTest : public cfixcc::TestFixture
    {
    public:
      void TestOne() 
      {
    	Calculator cal;
    	int res = cal.add(3, 3);
    	CFIXCC_ASSERT_EQUALS(6, res);
      }
      
      void TestTwo() 
      {}
    };
    
    CFIXCC_BEGIN_CLASS( ExampleTest )
      CFIXCC_METHOD( TestOne )
      CFIXCC_METHOD( TestTwo )
    CFIXCC_END_CLASS()
    This code give the following error message when building:
    1>------ Build started: Project: Test, Configuration: Debug Win32 ------
    1>Build started 25-03-2011 22:03:12.
    1>InitializeBuildStatus:
    1> Touching "Debug\Test.unsuccessfulbuild".
    1>ClCompile:
    1> All outputs are up-to-date.
    1> All outputs are up-to-date.
    1> Test.cpp
    1>ManifestResourceCompile:
    1> All outputs are up-to-date.
    1>Link:
    1> Creating library C:\Users\Jannick\Documents\Visual Studio 2010\Projects\Calculator\Debug\Test.lib and object C:\Users\Jannick\Documents\Visual Studio 2010\Projects\Calculator\Debug\Test.exp
    1>Test.obj : error LNK2019: unresolved external symbol "public: __thiscall Calculator::~Calculator(void)" (??1Calculator@@QAE@XZ) referenced in function "public: void __thiscall ExampleTest::TestOne(void)" (?TestOne@ExampleTest@@QAEXXZ)
    1>Test.obj : error LNK2019: unresolved external symbol "public: int __thiscall Calculator::add(int,int)" (?add@Calculator@@QAEHHH@Z) referenced in function "public: void __thiscall ExampleTest::TestOne(void)" (?TestOne@ExampleTest@@QAEXXZ)
    1>Test.obj : error LNK2019: unresolved external symbol "public: __thiscall Calculator::Calculator(void)" (??0Calculator@@QAE@XZ) referenced in function "public: void __thiscall ExampleTest::TestOne(void)" (?TestOne@ExampleTest@@QAEXXZ)
    1>C:\Users\Jannick\Documents\Visual Studio 2010\Projects\Calculator\Debug\Test.dll : fatal error LNK1120: 3 unresolved externals
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:01.13
    ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

    I cant understand why its not possible for me to simply create an object of type Calculator and then ude its method add(int, int).

    (Bonus question :-) Why does the Calculator.cpp have to have a main method? Is is possible to just make a class that have usable methods like my add(int, int) and not have a main method?)

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Projects are self-contained.

    If you want to reuse source (or compiled objects) from one project in another, then you need to add the relevant file to the project. For example, add Calculator.cpp to the project named Test.

    The need for a main() function is standard C++. All executables need a logical starting point. When creating non-console applications (eg your second project) you do not need a main() function, because the framework provides such a starting point for you. The difference comes down to different project settings.
    Last edited by grumpy; 03-25-2011 at 03:27 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since you're getting linker errors about unresolved symbols for calculator, I think something is wrong with your include statements/project.

    If the calculator is part of the project that contains the code in question, then
    #include "Calculator.h"
    should be good enough. It worked for the source containing the implementation of Calculator, so I don't see why it wouldn't work for a unit test.

    FYI,
    #include "something.hpp" looks for something.hpp in project folders;
    #include <something.hpp> checks the compiler's include directories first, then also any paths to something.hpp on the command line. So that is the difference. For example, g++ has the -I switch to provide paths to header files.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your unit tests should not be created in another project. Create them inside the project which contains the source you want to create a test for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Quote Originally Posted by Elysia View Post
    Your unit tests should not be created in another project. Create them inside the project which contains the source you want to create a test for.
    Thank you! This works great. Creating the test in the same project seems to be the best approach for me.

    Thank you for the other answers.

    Quick question though. I can instantiate objects in the test method, so it works now, but i cannot figure out how Before() is used:
    Code:
    ...
            void Before()
    	{
    		Calculator cal;
    	}
    
    	void Test()
    	{
    		int res = cal.add(3, 3);
    		CFIXCC_ASSERT_EQUALS(6, res);
    	}
    ...
    The compiler cant read the cal variable in the test method. I cant find the explanation for this problem :-/

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Are you people kidding? Of course you can use one project in another project.

    Say you've got a library, Project A. You have a unit test, Project B. You want to use the unit test to test the code from Project A.

    Put both projects in the same solution. Right click on Project B. Select "Project Dependencies..." Click the checkbox next to Project A.

    Done.

    EDIT: Okay, not quite done. You need to adjust the include path of Project B so it can see the headers from Project A.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by animation View Post
    The compiler cant read the cal variable in the test method. I cant find the explanation for this problem :-/
    Well, of course it doesn't work. The unit test also has to follow C++ scope rules. What you create in one function cannot possibly exist in another function (unless it's created on the heap, but then you have the same problem with the pointer to the object).
    What you basically have to do is create the calculator as a member of your class. Then you perform any initialization you want or need before the tests in the Before member function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Quote Originally Posted by Elysia View Post
    Well, of course it doesn't work. The unit test also has to follow C++ scope rules. What you create in one function cannot possibly exist in another function (unless it's created on the heap, but then you have the same problem with the pointer to the object).
    What you basically have to do is create the calculator as a member of your class. Then you perform any initialization you want or need before the tests in the Before member function.
    Well ofcause. Thank you :-) I think i was blinded by all the other dificalties i had with assertions - this one i should have figured out my self.

    To brewbuck: Thanks, this works too, but then you have to incude all the headerfiles you are working with. But this might be a cleaner solution actually.

Popular pages Recent additions subscribe to a feed