[英]C++ Calling function inside another function
In my C++ class we've been given an assignment to make a coin toss program that has the random number generator in one function and it is called into another function that runs it twelve(12) times. 在我的C ++类中,我们已经被赋予了一个分配来制作一个抛硬币程序,该程序在一个函数中具有随机数生成器,并且它被调用到运行它十二(12)次的另一个函数中。
int cointToss()
{
return rand()%2;
}
int run12()
{
int face, heads=0;
for (int i=0; i<12; i++)
{
face=coinToss();
if(face==1)
{
heads=heads+1;
}
}
return heads;
}
Whenever I try to run it however I keep getting this error, "1>source.obj : error LNK2001: unresolved external symbol "int __cdecl coinToss(void)" (?coinToss@@YAHXZ)" 每当我尝试运行它时,我会不断收到此错误,“1> source.obj:错误LNK2001:未解析的外部符号”int __cdecl coinToss(void)“(?coinToss @@ YAHXZ)”
I can't seem to find a resource saying how to correctly call the first function inside of the second. 我似乎无法找到一个资源说如何正确调用第二个函数内部的第一个函数。
Well, this is embarrassing. 嗯,这很尴尬。
You made a typo. 你弄错了。 The function is called "cointToss", but you are calling "coinToss" (see the extra t?).
该函数被称为“cointToss”,但您正在调用“coinToss”(请参阅额外的t?)。
C implicitly added a function declaration for you. C隐式为您添加了一个函数声明。 Turn on warning, and you'll see.
打开警告,你会看到。
Fix your typo, and the world will go round again. 解决你的错字,世界将再次出现。
您的方法声明为cointToss
,但您使用coinToss
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.