愚蠢的问题(8行代码) [英] Silly question (8 lines of code)

查看:75
本文介绍了愚蠢的问题(8行代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不起作用?

-----------------------------

class CRectangle {

public:

void set_values();

};


void CRectangle :: set_values(){}


CRectangle rect;

rect.set_values();


int main(){}

------------------------------

这项工作?


-----------------------------

class CRectangle {

public:

void set_values();

};


void CRectangle :: set_values(){}

CRectangle rect;

int main(){rect.set_values();}

------------------------------


谢谢!

Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------
And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
int main () {rect.set_values ();}
------------------------------

Thanks!

推荐答案

Manuel写道:
为什么这不起作用?
--------- --------------------
class CRectangle {
public:
void set_values();
};

void CRectangle :: set_values(){ }

CRectangle rect;
rect.set_values();

int main(){}
---------- --------------------

这项工作?

---------- -------------------
class CRectangle {
public:
void set_values();
};

void CRectangle :: set_values(){}

CRectangle rect;

int main(){rect.set_values();}
- -----------------------------

谢谢!
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------
And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
int main () {rect.set_values ();}
------------------------------

Thanks!



因为你在程序流程之外做工作。

函数/方法之外的区域用于诸如声明之类的事情,而不是实际工作。



Because you are doing work outside of program flow. Areas outside of
functions/methods are for things such as declarations, not actual work.


Manuel写道:
为什么这不起作用?
-----------------------------
class CRectangle {
public:
void set_values();
};

void CRectangle :: set_values(){}

CRectangle rect;
rect .set_values();

int main(){}
--------------------------- ---

这项工作?

--------------------------- -
class CRectangle {
public:
void set_values();
};

void CRectangle :: set_values(){}

CRectangle rect;

int main(){rect.set_values();}
------------------ ------------

谢谢!
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------
And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
int main () {rect.set_values ();}
------------------------------

Thanks!



你好,


在你的第一个非工作的例子,出了什么问题(请提供

完整的错误信息)你对p的期望是什么? rogram?我的猜测是,b $ b猜测编译器生成的代码什么也没用,因为你的主要内容是什么?$ / b

祝你好运,

彼得


Hello,

In your first non-working example, what is going wrong (please provide
the complete error message) and what do you expect from the program? My
guess is that the compiler produces code that does nothing since you do
nothing in your main?

Best regards,
Peter


Manuel写道:
为什么这不起作用?
------ -----------------------
class CRectangle {
public:
void set_values();
} ;

void CRectangle :: set_values(){}

CRectangle rect;
rect.set_values();

int main( ){}
------------------------------

这项工作?<

-----------------------------
class CRectangle {
public:
void set_values();
};

void CRectangle :: set_values(){}

CRectangle rect;

int main(){rect.set_values();}
------------------------------

谢谢!
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------
And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
int main () {rect.set_values ();}
------------------------------

Thanks!




一个程序是*序列*的指令,用C ++表示当

全部在itialization完成,调用main()。所有程序行为

必须来自这两个步骤之一。在你的第一个例子中,对CRectangle :: set_values()的

调用不适合该方案,因为它不是初始化的一部分,也不是调用的后果主要()。在

第二个例子中,它是后者。您可以将

的调用部分作为预主要初始化,如下所示:


struct CRectangle

{

bool set_values(){return true; } $ / $
};


命名空间//文件范围变量的匿名命名空间

{

CRectangle rect;

const bool init = rect.set_values();

}


int main(){}


干杯! --M



A program is a *sequence* of instructions, which in C++ means that when
all initialization is complete, main() is invoked. All program behavior
must result from one of those two steps. In your first example, the
call to CRectangle::set_values() doesn''t fit in that scheme since it is
neither part of initialization nor a consequence of calling main(). In
the second example, it is the latter. You could make the call part of
the pre-main initialization like this:

struct CRectangle
{
bool set_values() { return true; }
};

namespace // anonymous namespace for file-scope variables
{
CRectangle rect;
const bool init = rect.set_values();
}

int main() {}

Cheers! --M


这篇关于愚蠢的问题(8行代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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