VivekDev
VivekDev

Reputation: 25369

Use a function in different file and different namespace in Visual C++

I am struggling with my first steps in C++. Already asked this question, but did not get the full answer specifically about namespace.

I did the following.

  1. In Visual Studio 2015 created am empty project(New Project -> Visual C++ -> Empty Project)
  2. Then I added two files in Source.cpp and PrintFunc.cpp whose respective contents are as follows.

Source.cpp

#include <iostream>

using namespace std;

int PrintHello();
extern int tempCount;

void main()
{
    int i;
    PrintHello();
    cout << tempCount << endl;
    cout << "Hello from main" << endl;
}

PrintFunc.cpp

#include <iostream>

using namespace std;

int tempCount = 111;

int PrintHello()
{
    cout << "Hello from Source1" << endl;
    return 0;
}

This is compiling perfectly. Now I am learning about namespaces, so I just tried to put the contents of the second file in a namespace as follows.

PrintFunc.cpp (modified)

#include <iostream>

using namespace std;

namespace MyNameSpace
{
    int tempCount = 111;

    int PrintHello()
    {
        cout << "Hello from Source1" << endl;
        return 0;
    }
}

And now I modified the Source.cpp also to reflect the namespaces introduction in the previous snippets.

#include <iostream>

using namespace std;

int MyNameSpace::PrintHello();
extern int MyNameSpace::tempCount;

void main()
{
    int i;
    PrintHello();
    cout << tempCount << endl;
    cout << "Hello from main" << endl;
}

This simply does not compile. Can someone please kindly correct me. My objective is to understand namespace concept in c++. Also I have good exp with C#.

Upvotes: 4

Views: 2558

Answers (3)

VivekDev
VivekDev

Reputation: 25369

So finally here it is that I have settled for based on Dmitriy Zapevalov answer.

First Print.h

#pragma once

namespace MyNameSpace
{
    int PrintHello();
    extern int tempCount;
}

Next PrintFunc.cpp

#include <iostream>
#include "Print.h"

using namespace std;

namespace MyNameSpace
{
    int tempCount = 111;
    int PrintHello()
    {
        cout << "Hello from Source1" << endl;
        return 0;
    }
}

PrintFunc.cpp can also be like this as an alternative.

PrintFunc.cpp (alternative)

#include <iostream>
#include "Print.h"

using namespace std;

int MyNameSpace::tempCount = 111;
int MyNameSpace::PrintHello()
{
    cout << "Hello from Source1" << endl;
    return 0;
}

And finally Source.cpp

#include <iostream>
#include "Print.h"

using namespace std;
using namespace MyNameSpace;

void main()
{
    PrintHello();
    cout << tempCount << endl;
    cout << "Hello from main" << endl;
}

Source.cpp can also be like this as an alternative.

Source.cpp (alternative)

#include <iostream>
#include "Print.h"

using namespace std;

void main()
{
    MyNameSpace::PrintHello();
    cout << MyNameSpace::tempCount << endl;
    cout << "Hello from main" << endl;
}

Upvotes: 0

Dmitriy Zapevalov
Dmitriy Zapevalov

Reputation: 1357

The problem that compiler does not know about namespace MYSpace when compiling Source.cpp.

#include <iostream>

using namespace std;

namespace MyNameSpace
{
    int PrintHello();
    extern int tempCount;
}
int main()
{
    int i;
    MyNameSpace::PrintHello();
    cout << MyNameSpace::tempCount << endl;
    cout << "Hello from main" << endl;
}

But this sample is useless. It work only because you have only one consumer .cpp.

You should use .h file and then include it (PrintFunc.h) in Source.cpp and any other .cpp when you want to use that funtions.

I'll write an example:

Print.h

#pragma once

namespace MyNameSpace
{
    int PrintHello();
    extern int tempCount;
}

Notice that we dont't use additional includes and using namespace here. We would use includes only to use some classes in functions interfaces. using namespace could "spoil" consumer's .cpp or .h.

Print.cpp

#include <iostream>
#include "Print.h"

using namespace std;

int MyNameSpace::tempCount = 111;

int MyNameSpace::PrintHello()
{
    cout << "Hello from Source1" << endl;
    return 0;
}

Here we can set any include it will not touch any other files.

And consumer .cpp:

#include <iostream>
#include "Print.h"

using namespace std;

int main()
{
    int i;
    MyNameSpace::PrintHello();
    cout << MyNameSpace::tempCount << endl;
    cout << "Hello from main" << endl;
}

VS specific: #pragma once and for VS you have to #include "stdafx.h" at first line in any .cpp

Upvotes: 8

Ari0nhh
Ari0nhh

Reputation: 5920

You have to supply namespace name when using its members. Or use using namespace directive.

void main()
{
    int i;
    MyNameSpace::PrintHello();
    cout << MyNameSpace::tempCount << endl;
    cout << "Hello from main" << endl;
}

However, for it to work namespace should be declared in the separate .h file and it should be included in your source.cpp

Upvotes: 4

Related Questions