“使用”的优先顺序和#include [英] precedence of "using" and #include

查看:46
本文介绍了“使用”的优先顺序和#include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的系统上编译以下作品:


文件main.cpp:


#include< iostream>


名称空间{

int foo;

}


main()

{

使用space :: foo;

#include" fragment"

cout<< foo<< endl;

}


文件片段:


foo = 3;


我的问题是,有什么理由它不应该起作用,因为

使用语句需要在文件片段中? (我相信它应该没关系,但我正试图破解与我的不同环境中的开发人员的沟通



这些行。)


谢谢,

史蒂夫

解决方案

Steve Pope写道:


在我的系统上编译以下工作:


file main.cpp:


#include< iostream>


命名空间空间{

int foo;

}


main()

{

使用space :: foo;

#include" fragment"

cout<< foo<< endl;

}


文件片段:


foo = 3;



#include< iostream>


名称空间{

int foo;

}


int main()

{

使用space :: foo;

foo = 3;

std :: cout<< foo<< std :: endl;

}


我的问题是,有什么理由不应该工作因为

使用语句需要在文件片段中? (我相信它应该没关系,但我正试图破解与我的不同环境中的开发人员的沟通



这些行。)


谢谢,

Steve


Steve Pope写道:


在我的系统上编译以下工作:


file main.cpp:

#include< iostream>

命名空间空间{

int foo;

}


main()

{

使用space :: foo;

#include" fragment"

cout<< foo<< endl;

}


文件片段:


foo = 3;


我的问题是,有什么理由它不应该起作用,因为

使用语句需要在文件片段中?



是的。你的主要功能应该返回一个int,你必须符合

cout和endl与std ::


ben


benben< benhonghatgmaildotcom @nospamwrote:


> Steve Pope写道:


>我的问题是,有什么理由它不应该起作用,因为
使用语句需要在文件片段中?


>是的。你的主要功能应该返回一个int,你必须用std ::
来限定cout和endl



好​​的,这不是我的问题,而且你提到的

点我的系统很自由。更正了以下代码。所以让我再问一次。

是否使用space :: foo;需要在文件片段中吗?谢谢。


史蒂夫


***********


文件main.cpp:


#include< iostream>

名称空间{

int foo; < br $>
}


int

main()

{

使用space :: foo;

#include" fragment"

std :: cout<< foo<< std :: endl;

}


文件片段:


foo = 3;


Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"? (I
believe it should be okay but I''m trying to decipher a communication
from a developer in a different environment than mine, along
these lines.)

Thanks,
Steve

解决方案

Steve Pope wrote:

Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

#include <iostream>

namespace space {
int foo;
}

int main()
{
using space::foo;
foo = 3;
std::cout << foo << std::endl;
}

My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"? (I
believe it should be okay but I''m trying to decipher a communication
from a developer in a different environment than mine, along
these lines.)

Thanks,
Steve


Steve Pope wrote:

Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"?

Yes. Your main function should return an int and you have to qualify
cout and endl with std::

ben


benben <benhonghatgmaildotcom@nospamwrote:

>Steve Pope wrote:

>My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"?

>Yes. Your main function should return an int and you have to qualify
cout and endl with std::

Okay, that wasn''t my question, and my system is liberal on the
points you mention. Corrected code below. So let me ask again.
Does "using space::foo;" need to be in the file fragment? Thanks.

Steve

***********

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

int
main()
{
using space::foo;
#include "fragment"
std::cout << foo << std::endl;
}

file fragment:

foo = 3;


这篇关于“使用”的优先顺序和#include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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