-1

I can't get this simple C++ code to work:

int secInt = 5;

double rubbish = secInt/60;

double factor = floor(rubbish);

I always get "ERROR: initializer element is not constant" from line 3

Factor always returns 0.00 in the log

Could anyone help with this, I'm feeling that I overlooked something very simple.

An example of this error can be seen here: http://ideone.com/2Wrkr9

4

3 回答 3

2

您的代码应该在main您的程序部分内:

int main()
{
    int secInt = 583;

    double rubbish = secInt/60.0;

    double factor = floor(rubbish);
}

您还应该使用标准 C++ 头文件,例如<cmath>.

于 2013-04-21T02:35:36.290 回答
1

您忘记将代码包含在函数中。

尝试这个。

int main()
{
    int secInt = 5;

    double rubbish = secInt/60.0;

    double factor = floor(rubbish);
}

C++ 将开始在名为 的函数中执行代码main,并从那里调用从 main 调用的任何函数。

于 2013-04-21T02:36:12.773 回答
1
double rubbish = secInt/60;

应该

double rubbish = static_cast<double>(secInt)/60;

因为secInt =5,所以floor(rubbish)应该0与上述更正一致。

main如果源文件中没有任何其他函数,则至少应该有一个函数。main是入口点。

int main()
{
 int secInt = 5;
 double rubbish = static_cast<double>(secInt)/60.0;

 double factor = floor(rubbish);
 return 0;
}
于 2013-04-21T02:36:20.270 回答