error: expected unqualified-id before "do"

This is a discussion on error: expected unqualified-id before "do" within the C++ Programming forums, part of the General Programming Boards category; Hi I'm trying to make a class with template functions in them. I get it to compile but during execution ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    error: expected unqualified-id before "do"

    Hi

    I'm trying to make a class with template functions in them. I get it to compile but during execution I get this error.

    test.h
    Code:
    class TestC
    {
    public:
    	TestC();
    
    	virtual ~TestC();
    
    public: // template Checks.
    	template <typename Evaluation> static bool check(Evaluation const& evaluation);
    };
    test.cpp
    Code:
    template <typename Evaluation>
    bool TestEvaluationHandlerC::check(Evaluation const& evaluation)
    {
    	return evaluation;
    }
    Code:
    void main ()
    {	
    	TestC * test = new TestC();
    	if(test->check(true))
    	{
    		cout << "true";
    	}
    	else
    	{
    		cout << "false";
    	}
    	delete test;
    }
    I'm new to templates so not really sure I get the syntax right but I have tried some different approaches without success.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    What error did you get? I suspect that you got a linker error, and the solution is to move the definition of TestEvaluationHandlerC::check into the header file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't use void main() -- use int main(). SourceForge.net: Void main - cpwiki

    It seems to compile for me if I change
    Code:
    template <typename Evaluation>
    bool TestEvaluationHandlerC::check(Evaluation const& evaluation)
    to
    Code:
    template <typename Evaluation>
    bool TestC::check(Evaluation const& evaluation)
    Keep in mind that you should probably either put the template code definition (the actual code) into the header file, or else use instantiation.

    Maybe you should post more code, along with the exact line which causes the error message.

    [edit] @laserlight: the error message seems to be in the title: error: expected unqualified-id before "do"

    I don't see any do-while loops, hence the request for more code . . . [/edit]
    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.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Ok made a unittest instead so posting that code with some modifications now. getting the following errors:
    error: expected unqualified-id before "do"
    error: expected unqualified-id before "while"

    these points out the header template setup.

    header file
    Code:
    class TestEvaluationHandlerC
    {
    public:
    	TestEvaluationHandlerC();
    
    	virtual ~TestEvaluationHandlerC();
    
    public: // template Checks.
    	template <typename Evaluation> bool CHECK(Evaluation const& evaluation);
    };
    source file:
    Code:
    TestEvaluationHandlerC::TestEvaluationHandlerC()
    {
    
    }
    
    TestEvaluationHandlerC::~TestEvaluationHandlerC()
    {
    
    }
    
    template <typename Evaluation>
    bool TestEvaluationHandlerC::CHECK(Evaluation const& evaluation)
    {
    	return evaluation;
    }
    unittests:
    Code:
    TEST(TestEvaluationHandlerC_001)
    {
    	TestEvaluationHandlerC * test = new TestEvaluationHandlerC();
    	CHECK_EQUAL( true, test->CHECK(true) );
    	CHECK_EQUAL( false, test->CHECK(false) );
    	delete test;
    }
    problem might be running it with unittests.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by scrier
    Ok made a unittest instead so posting that code with some modifications now. getting the following errors:
    error: expected unqualified-id before "do"
    error: expected unqualified-id before "while"
    (...)
    problem might be running it with unittests.
    Yes, I too have a hunch that your problem is directly related to the unit test framework that you are using. A function style macro might be defined with the help of a do while loop that only runs once, in order that it not interfere with the surrounding code in the places where it is used. But if there is a mistake with the use of the macro, this may then be manifested as a mistake related to a do while loop.

    Now, your unit test framework may have defined a macro named CHECK. This can intefere with your member function template of the same name. The solution is to stick to the convention that macro names should be fully capitalised. Rename your member function template, e.g.,
    Code:
    template <typename Evaluation> bool Check(Evaluation const& evaluation);
    Make the corresponding changes and compile. If the compilation fails, post the exact error message, including line numbers, and tell us what are the lines that they refer to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unqualified name in template
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2008, 03:48 AM
  2. qualified name and unqualified name
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2008, 07:41 AM

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