函数在调用之前声明但在之后定义 - 编译失败 [英] Function declared before call but defined after - compilation fails
问题描述
大家好,
我正在做另一项运动(我再说一遍,*运动*)。 (与这个
讨论无关)点是显示你可以注入一个朋友声明
到
a命名空间通过在封闭的类中声明它。我已成功完成
这个
,但请考虑以下程序:
//:C10:FriendInjection.cpp < br $>
//来自Thinking in C ++,2nd Edition
//可在 http://www.BruceEckel.com
//(c)Bruce Eckel 2000
// Copyright.txt中的版权声明
名称空间我{
class Us {
// ...
public:
朋友无效();
};
}
// 1
无效我::你(){}
int main(){
Me :: you();
返回0;
}
// 2
//在main()编译后定义你()
// void Me :: you(){}
///:〜
///////////////// ////////////////////////////////////////////////// //////
如果在::()之后在me(:)中定义*,则在(2)中调用*,gcc 4.1.1
说:
& 错误:''你''不是''我''的成员。相反,如果我::你是
*定义*在(1),
然后它编译。<br />
MSVC ++正如预期的那样,Express以任何一种方式编译它没有问题。
肯定这是gcc中的一个错误?
< jo ********** @ hotmail.comwrote in message
news:11 ***** ****************@q23g2000hsg.googlegro ups.com ...
Hello All,
我正在做另一个练习(我再说一遍,*练习*)。 (与这个
讨论无关)点是显示你可以注入一个朋友声明
到
a命名空间通过在封闭的类中声明它。我已成功完成
这个
,但请考虑以下程序:
//:C10:FriendInjection.cpp < br $>
//来自Thinking in C ++,2nd Edition
//可在 http://www.BruceEckel.com
//(c)Bruce Eckel 2000
// Copyright.txt中的版权声明
名称空间我{
class Us {
// ...
public:
朋友无效();
};
}
// 1
无效我::你(){}
int main(){
Me :: you();
返回0;
}
// 2
//在main()编译后定义你()
// void Me :: you(){}
///:〜
///////////////// ////////////////////////////////////////////////// //////
如果在::()之后在me(:)中定义*,则在(2)中调用*,gcc 4.1.1
说:
"错误:''你''不是''我''的成员。相反,如果我::你是
*定义*在(1),
然后它编译。<br />
MSVC ++正如预期的那样,Express以任何一种方式编译都没有问题。
肯定这是gcc中的一个错误?
为什么?
将某个函数声明为朋友不会声明或定义该函数。
它只是声明所述函数是朋友。
在情况(1)中,你声明*和*定义Me :: you()。如果你在main()之后将它移动到
,就像在情况(2)中一样,那么当编译main()时,Me :: you()还没有
声明,所以这是一个错误。
-Howard
5月16日上午11:38 ,johnbrown ... @ hotmail.com写道:
Hello All,
我正在做另一个练习(我再说一遍) ,*运动*)。 (与这个
讨论无关)点是显示你可以注入一个朋友声明
到
a命名空间通过在封闭的类中声明它。我已成功完成
这个
,但请考虑以下程序:
//:C10:FriendInjection.cpp < br $>
//来自Thinking in C ++,2nd Edition
//可用http://www.BruceEckel.com
//(c)Bruce Eckel 2000
// Copyright.txt中的版权声明
namespace Me {
class Us {
// ...
公开:
朋友无效();
};}
// 1
无效我::你(){}
int main(){
Me :: you( );
返回0;
}
// 2
/ /在main()编译后定义你()
// void Me :: you(){}
///:〜
//////////////////////////////////////////////// /////////////////////////
如果Me :: you()*定义* at( 2)在main()调用之后,gcc 4.1.1
说:
"错误:''你''不是''M的成员E" ;.相反,如果我::你是
*定义*在(1),
然后它编译。<br />
MSVC ++正如预期的那样,Express以任何一种方式编译都没有问题。
肯定这是gcc中的一个错误?
如果将其更改为
int main(){
Me: :我们::你();
返回0;
我会认为我们是是必要的。
" JLS" < de ******** @ yahoo.comwrote in message
news:11 ********************** @ n59g2000hsh.googlegr oups.com ...
5月16日上午11:38,johnbrown ... @ hotmail.com写道:
< blockquote class =post_quotes>
> Hello All,
我正在做另一个练习(我再说一遍,*练习*)。 (与此讨论无关)点是表明你可以通过在一个封闭的类中声明它来将一个朋友声明注入一个名称空间。我已成功完成了这个
,但请考虑以下程序:
//:C10:FriendInjection.cpp
//来自C ++思考,第2版
//可通过http://www.BruceEckel.com获取
//(c)Bruce Eckel 2000
// Copyright.txt中的版权声明
名称空间Me {
上课我们{
// ...
公众:
朋友无效();
};}
// 1 > void Me :: you(){}
int main(){
我::你();
返回0;
}
// 2
//在main()编译后定义你()
// void Me :: you(){}
/// :〜
//////////////////////////////////////////// /////////////////////////////
如果我::你()是*定义*在( 2)在main()调用之后,gcc 4.1.1
说:
错误:''你''不是''我''的成员。相反,如果Me ::你在*(1)定义*,那么它就会编译。
MSVC ++ Express可以正常编译它,没有问题,正如预期的那样。
这肯定是gcc中的一个bug?
如果你改成它会发生什么
int main(){
我: :我们::你();
返回0;
我会认为我们是是必要的。
嗯?你()不是我们的成员的功能。这是我们的朋友*。
-Howard
Hello All,
I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:
//: C10:FriendInjection.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};
}
// 1
void Me::you(){}
int main() {
Me::you();
return 0;
}
// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////
If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: ''you'' is not a member of ''Me''". If, instead, Me::you is
*defined* at (1),
then it compiles.
MSVC++ Express compiles it either way without a problem, as expected.
Surely this is a bug in gcc?
<jo**********@hotmail.comwrote in message
news:11*********************@q23g2000hsg.googlegro ups.com...Hello All,
I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:
//: C10:FriendInjection.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};
}
// 1
void Me::you(){}
int main() {
Me::you();
return 0;
}
// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////
If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: ''you'' is not a member of ''Me''". If, instead, Me::you is
*defined* at (1),
then it compiles.
MSVC++ Express compiles it either way without a problem, as expected.
Surely this is a bug in gcc?
Why?
Declaring a function as a friend does not declare or define that function.
It merely states that said function is a friend.
In case (1), you''re declaring *and* defining Me::you(). If you move that to
after main() as in case (2), then when main() is compiled, Me::you() has not
yet been declaredand so it''s an error.
-Howard
On May 16, 11:38 am, johnbrown...@hotmail.com wrote:Hello All,
I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:
//: C10:FriendInjection.cpp
// From Thinking in C++, 2nd Edition
// Available athttp://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};}
// 1
void Me::you(){}
int main() {
Me::you();
return 0;
}
// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////
If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: ''you'' is not a member of ''Me''". If, instead, Me::you is
*defined* at (1),
then it compiles.
MSVC++ Express compiles it either way without a problem, as expected.
Surely this is a bug in gcc?What happens if you change it to
int main() {
Me::Us::you();
return 0;
I would have thought that the "Us" would be necessary.
"JLS" <de********@yahoo.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...On May 16, 11:38 am, johnbrown...@hotmail.com wrote:>Hello All,
I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:
//: C10:FriendInjection.cpp
// From Thinking in C++, 2nd Edition
// Available athttp://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};}
// 1
void Me::you(){}
int main() {
Me::you();
return 0;
}
// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////
If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: ''you'' is not a member of ''Me''". If, instead, Me::you is
*defined* at (1),
then it compiles.
MSVC++ Express compiles it either way without a problem, as expected.
Surely this is a bug in gcc?
What happens if you change it to
int main() {
Me::Us::you();
return 0;
I would have thought that the "Us" would be necessary.
Huh? The function you() is not a member of Us. It''s a *friend* of Us.
-Howard
这篇关于函数在调用之前声明但在之后定义 - 编译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!