namespaces, classes and free functions - when do you need fully qualified names

In my example below, why do I have to fully qualify the name of the free function in the cpp to avoid linker errors and why does it work for the class function without? Can you explain the difference?

ctest.h:

namespace Test
{
    int FreeFunction();

    class CTest
    {
        public:
            CTest();
            ~CTest();
    };
}

ctest.cpp:

#include "ctest.h"

using namespace Test;

// int FreeFunction()     -> undefined reference error
int Test::FreeFunction()  -> works just fine
{
    return 0;
}

CTest::CTest()                -> no need to fully qualify name, i.e. Test::CTest
{}

CTest::~CTest()
{}

Thanks for your time & help.

Answers


int FreeFunction(void);  

is just a declaration whereas the below is a definition.

class CTest 
{ 
    public: 
        CTest(); 
        ~CTest(); 
}; 

If you want to provide definition for an already declared entity in a namespace (e.g. in an enclosing namespace), it has to be fully qualified name.

EDIT2:

Here is something that would give you some more clarity. Note no using directive in this code.

namespace Test { 
    int FreeFunction(void);   // declare

    class CTest;              // declare
} 

int Test::FreeFunction(){return 0;} // define
class Test::CTest{            // define
};

int main(){}

EDIT 3: Declaration vs Definition (C++0x) $3.1/2-

A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a function-body, it declares a static data member in a class definition (9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attribute-declaration (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).


Need Your Help

Rspec Authorization in Rails

ruby-on-rails rspec http-headers

I'm developing a rails API, using rails 4, and I want to test it. To do it, I am using Rspec framework.

Strange things happening to my table view

iphone ios uitableview uibutton uilabel

I have a table view. I originally used the cell's textLabel to handle this, but that didnt work, so i added a UILabel to each cell programatically and i have two buttons a add button and a subtract

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.