在Visual C ++中使用不同文件和不同命名空间中的函数 [英] Use a function in different file and different namespace in Visual C++

查看:214
本文介绍了在Visual C ++中使用不同文件和不同命名空间中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中遇到了我的第一步。已询问问题,但没有得到关于命名空间的完整答案。



我做了以下。


    <在Visual Studio 2015中创建了一个空项目(新建项目 - > Visual C ++ - >空项目)
  1. 然后我在Source.cpp和PrintFunc.cpp中添加了两个文件如下。

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;
}

这是完美的编译。
现在我正在学习命名空间,所以我试图把第二个文件的内容放在命名空间中,如下所示。



PrintFunc.cpp(modified)

  #include< iostream> 

using namespace std;

namespace MyNameSpace
{
int tempCount = 111;

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



现在我修改Source.cpp也反映

  #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;
}

这不会编译。有人可以请请纠正我。我的目标是在c ++中理解命名空间概念。

解决方案

编译器不知道命名空间的问题 MYSpace



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;
}

但这个示例是无用的。它只有因为你只有一个消费者 .cpp



你应该使用。 h 文件,然后将其( PrintFunc.h )包含在 Source.cpp



我会写一个例子:



Print.h

  #pragma once 

命名空间MyNameSpace
{
int PrintHello();
extern int tempCount;
}

请注意,我们不要使用额外的 / code>和使用命名空间。我们将使用包括仅使用函数中的一些类 interfaces
使用命名空间可以破坏消费者的 .cpp .h



Print.cpp

  #include < iostream> 
#includePrint.h

using namespace std;

int MyNameSpace :: tempCount = 111;

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

这里我们可以设置 include 它不会触及任何其他文件。



和消费者 .cpp

  #include< iostream> 
#includePrint.h

using namespace std;

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

VS特定: #pragma once 并且对于VS你必须在任何 .cpp 中第一行 #includestdafx.h / p>

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#.

解决方案

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

这篇关于在Visual C ++中使用不同文件和不同命名空间中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆