By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,673 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,673 IT Pros & Developers. It's quick & easy.

creating an object of a #defined class

P: n/a
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
testmapname(){}\
testmapname(std::ofstream 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

Sep 1 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
na**************@gmail.com wrote:
I am creating a #defined class like -
<snip really ugly macro>
Now I want to create a global object for this class. How can I do this?
Why are you doing this? Have you considered using a template instead?

Regards,
Bart.

Sep 1 '06 #2

P: n/a
na**************@gmail.com wrote:
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
testmapname(){}\
testmapname(std::ofstream resultfile)\
This won't work. std::ofstream 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'?

Sep 1 '06 #3

P: n/a
nandakumar.raghu wrote:
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.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 1 '06 #4

P: n/a
Bart wrote:
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.)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 1 '06 #5

P: n/a
na**************@gmail.com 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.

--

Frederick Gotham
Sep 1 '06 #6

P: n/a
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.

Rolf Magnus wrote:
na**************@gmail.com wrote:
Hi,

I am creating a #defined class like -

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

This won't work. std::ofstream 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'?
Sep 2 '06 #7

P: n/a
Please don't top-post. Rearranged.

na**************@gmail.com wrote:
Rolf Magnus wrote:
>na**************@gmail.com wrote:
Hi,

I am creating a #defined class like -

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

This won't work. std::ofstream 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.

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.

Sep 2 '06 #8

P: n/a

na**************@gmail.com wrote:
Hi,

I am creating a #defined class like -

#define BEGIN_TEST_MAP(testmapname) class testmapname\
{\
testmapname(){}\
testmapname(std::ofstream 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::ofstream& 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::ofstream x("a.txt");
a p(x);

return 0;
}

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

Sep 3 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.