Thread: error: expected unqualified-id before "do"

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,057
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08:41 AM