Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a test fixture class from a normal class with constructor declaration (with arguments) as shown below:

hello.h

class hello
{
public:
hello(const uint32_t argID, const uint8_t argCommand);
virtual ~hello();
void initialize();
};


where uint32_t is:
typedef unsigned int
and uint8_t is:
typedef unsigned char


My Test Fixture Class:

helloTestFixture.h

C++
class helloTestFixture:public testing::Test
{
public:
helloTestFixture(/*How to carry out the constructor declaration in this test fixture class corresponding to the above class?*/);
virtual ~helloTestFixture();
hello m_object;
    };
TEST_F(helloTestFixture, InitializeCheck) // Test to access the 'intialize' function
{
m_object.initialize();
}


After trying to implement the above code, it gives me the error:

Error C2512: 'hello': no appropriate default constructor available


I was trying to replicate the constructor constructed in the hello.h file into my hellotestfixture.h file. Any way around for doing that? I have tried implementing it in many ways but no success as of yet. Any suggestions on how to implement this?
Posted
Updated 2-Dec-11 0:28am
v3

1 solution

You may even define just the default helloTestFixture constructor, but you have to provide arguments to hello constructor for initializeing m_object. The following code
C++
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;

class hello
{
public:
  hello(const uint32_t argID, const uint8_t argCommand);
virtual ~hello();
void initialize();
};

hello::hello(const uint32_t argID, const uint8_t argCommand){/* do nothing*/}
hello::~hello(){/* do nothing*/}
void hello::initialize(){/* do nothing*/}

class helloTestFixture
{
public:
  helloTestFixture();
  virtual ~helloTestFixture();
  hello m_object;
};

helloTestFixture::helloTestFixture():m_object(0,0){/* do nothing */}
helloTestFixture::~helloTestFixture(){/* do nothing */}
   
int main()
{
    helloTestFixture htf;
    htf.m_object.initialize();
}

compiles and runs nicely.
 
Share this answer
 
v2
Comments
Mobile.Instinct 2-Dec-11 6:55am    
5 star Reply. Been struggling since two days. Amazing. Thank You so much.
CPallini 2-Dec-11 6:58am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900