creating an object of a #defined class

  • Thread starter nandakumar.raghu
  • Start date
N

nandakumar.raghu

Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
testmapname(){}\
testmapname(std::eek:fstream resultfile)\
{\
resultfile << "<Test name=\"" << #testmapname << "\"\n";
#define TEST_CASE(_test_case)\
resultfile << _test_case();
#define END_TEST_MAP \
resultfile << "</Test>";\
}\
};\

Now I want to create a global object for this class. How can I do this?
When I write

testmapname objectname(resfile);

then the compiler gives testmapname undefined and takes it as int.

so how do i create an object of this class. I only need to call the
constructor of this class.


Thanks

Nandakumar
 
R

Rolf Magnus

Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
testmapname(){}\
testmapname(std::eek:fstream resultfile)\

This won't work. std::eek:fstream is not copyable.
{\
resultfile << "<Test name=\"" << #testmapname << "\"\n";
#define TEST_CASE(_test_case)\
resultfile << _test_case();
#define END_TEST_MAP \
resultfile << "</Test>";\
}\
};\

Now I want to create a global object for this class. How can I do this?

What class? You only have a macro up to this point. You first have to use it
to define a class.
When I write

testmapname objectname(resfile);

then the compiler gives testmapname undefined and takes it as int.

Did you use the macro to define a class named 'testmapname'?
 
P

Phlip

nandakumar.raghu said:
I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\ ....
Now I want to create a global object for this class. How can I do this?
When I write

testmapname objectname(resfile);

then the compiler gives testmapname undefined and takes it as int.

so how do i create an object of this class. I only need to call the
constructor of this class.

The CppUnit source code shows all this in action. (Further, one should use a
much lighter test rig, such as UnitTest++, with a simple suite system.)

Given BEGIN_TEST_MAP(Foo), the class name is Foo. Either use that, or put
your testmapname objectname(resfile); line into the macro, with \ before it.
 
P

Phlip

Bart said:
Why are you doing this?

It's a common system to implement the Test Collector Pattern in a unit test
rig.

(All programmers write unit tests, so I must assume you are simply
unfamiliar with the pattern.)
 
F

Frederick Gotham

(e-mail address removed) posted:
Now I want to create a global object for this class. How can I do this?


You don't understand macros. A macro is simple text replacement _before
anything is compiled_.

Perform the text replacement manually yourself and you'll see any and all
error.
 
N

nandakumar.raghu

Yes, i am writing this macro to define a class for a particular test.
This way each test will have its own class when the macros for
BEGIN_TEST_MAP will be expanded. Now I need to call the constructor of
each class which actually executes the testcase _test_case();
So how do i create an object.

Writing testmapname objname; does not work as testmapname is not
defined beyond BEGIN_TEST_MAP.
 
R

Rolf Magnus

Please don't top-post. Rearranged.

Yes, i am writing this macro to define a class for a particular test.
This way each test will have its own class when the macros for
BEGIN_TEST_MAP will be expanded. Now I need to call the constructor of
each class which actually executes the testcase _test_case();
So how do i create an object.

Writing testmapname objname; does not work as testmapname is not
defined beyond BEGIN_TEST_MAP.

It is not defined at all. As I said, there is no class. The code just
defines a macro, nothing more. testmapname is a parameter to that macro and
has no meaning outside it. If you use the macro to define a class, you
specify the class's name as an argument to the macro. If you want that
class instantiated, you have to use that name.
 
P

Peyman

Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
testmapname(){}\
testmapname(std::eek:fstream resultfile)\
{\
resultfile << "<Test name=\"" << #testmapname << "\"\n";
#define TEST_CASE(_test_case)\
resultfile << _test_case();
#define END_TEST_MAP \
resultfile << "</Test>";\
}\
};\

Now I want to create a global object for this class. How can I do this?
When I write

testmapname objectname(resfile);

then the compiler gives testmapname undefined and takes it as int.

so how do i create an object of this class. I only need to call the
constructor of this class.


Thanks

Nandakumar

Here is the whole code you need!
#include <iostream>
#include <fstream>

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
public:\
testmapname()\
{\
std::cout << "just working!\n";\
}\
testmapname(std::eek:fstream& ofile)\
{\
ofile << "<Test name=\"" << #testmapname <<
"\"\n" <<

#define TEST_CASE(output)\
output() << "<\\Test>";\
}\
};

int foo()
{
return 0;
}

int main()
{
BEGIN_TEST_MAP(a)
TEST_CASE(foo);

std::eek:fstream x("a.txt");
a p(x);

return 0;
}

// note: do not put a ';' after BEGIN_TEST_MAP(a)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top