试着学习巴基的教程
trying to follow Bucky tutorials
在学习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()并没有真正生成一个真正的随机数,只是一个伪随机数
相关文章:
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何将经过训练的机器学习模型保存在python中并将其加载到C++中进行预测?
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的教程SFML代码在Ubuntu 16.04上崩溃
- 我在哪里可以找到 Clang 的教程
- C++ 有没有关于如何使用句柄的教程?
- WXWIDGETS和GUI的新手 - 学习材料?(WXWIDGETS 3.0,C ,Win 7,CodeBlocks
- 编译 boost 文件系统的教程文件时遇到问题
- 使用生成器和发行版扩展c++11随机的教程或示例代码
- 试着学习巴基的教程
- 有没有关于如何将包含文件夹和文件的zip文件解压缩到某个目录并通过MiniZip将其打包的教程
- c++Why是关于字符串的教程中使用的一组字符
- 编写简单脚本语言的教程或介绍
- 在掌握VB.NET的同时学习c++
- 关于Freetype缓存子系统的教程
- 以基于迭代器的方式学习C++:这对获得背景很好,但对获得工作实用吗
- 7-Zip:任何好的教程
- 在哪里可以获得关于创建mpeg-1视频编码器的教程
- 当我尝试运行svm的教程时,get_support_vector_count的功能无法正常工作