试着学习巴基的教程

trying to follow Bucky tutorials

本文关键字:的教程 学习      更新时间:2023-10-16

在学习Youtube教程时(请参阅下面的相关代码),我得到了以下错误:

错误行6
错误:在"("之前需要构造函数析构函数或类型转换

是什么原因导致了这个错误,我该如何解决?

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>
void myfun(int);//using own function
myfun(8);//pow(4.0,10.0)
using namespace std;
int main()
{
    double num1;
   srand(time(0));// to get a true random number
    double num2;
    num1 = pow(3.0, 9.0);//2 to the power of 4
    cout << num1 <<endl;
    num2 = rand() %100;//random number out of 100
    cout << "nrandom number = " << num2 << endl ;
    return 0;
}
void myfun(int x)
{
    using namespace std;
    cout << "my favourite number is " << x << endl;
}

这是一个声明:

void myfun(int);//using own function

这是一个函数调用:

myfun(8);//pow(4.0,10.0)

不能在上下文之外调用函数。

试着把它移到main里面。你想达到什么目的?

int main()
{
    myfun(8);  //<---- here
    double num1;
    srand(time(0));// to get a true random number
    double num2;
    num1 = pow(3.0, 9.0);//2 to the power of 4
    cout << num1 <<endl;
    num2 = rand() %100;//random number out of 100
    cout << "nrandom number = " << num2 << endl ;
    return 0;
}

正如Luchian所说,将函数调用移动到一个作用域中。。在这种情况下是main。我还有其他几点。请参阅以下代码。

    #include <iostream>
    #include <cmath>
    #include <stdlib.h>
    #include <time.h>
    void myfun(int);//using own function
    void myfun(int x)
    {
        std::cout << "my favourite number is " << x << std::endl;
    }
    int main()
    {
        double num1, num2;
        srand(time(0));// to get a true pseudo-random number
        num1 = pow(3.0, 9.0);//2 to the power of 4
        std::cout << num1 << std::endl;
        num2 = rand() %100;//random number out of 100
        std::cout << "nrandom number = " << num2 << std::endl ;
        myfun(8);//pow(4.0,10.0)
        return 0;
    }

耦合点:

  • 通常认为在全局范围内执行using namespace std;是个坏主意。最好根据需要将std附加到名称中,以避免混淆命名空间并避免名称冲突
  • srand()并没有真正生成一个真正的随机数,只是一个伪随机数