C+中未定义引用的编译错误+; 我通过程序学习C++,并从教科书中做了关于随机数实现的例子。有一个头文件: #ifndef _random_h #define _random_h /* * Function randomInteger * Usage: int n = randomInteger(low, high); * --------------------------------------- * returns a random integer in the range low to high, inclusive. */ int randomInteger(int low, int high); /* * Function: randomReal * Usage: double d = randomReal(low, high) * --------------------------------------- * Returns a random real number in the half-open interval [low, high). A * half-open interval includes the first endpoint but not the second, which * means that the result is always greater than or equal to low but * strictly less than high. */ double randomReal(double low, double high); /* * Function: randomChance * Usage: if(randomChance(p)) ... * ------------------------------- * Returns true with the probability indicated by p. The argument p must * be a floating-point number between 0 (never) and 1 (always). For * example, calling randomChance(.30) returns true 30 percent of time. */ bool randomChance(double p); /* * Function: setRandomSeed * Usage: setRandomSeed(seed); * --------------------------- * Sets the internal random number seed to the specified value. You can * use this function to set a specific starting point for the pseudorandom * sequence or to ensure that program behavior is repeatable during the * debugging phase. */ void setRandomSeed(int seed); #endif
以及源文件本身:C+中未定义引用的编译错误+; 我通过程序学习C++,并从教科书中做了关于随机数实现的例子。有一个头文件: #ifndef _random_h #define _random_h /* * Function randomInteger * Usage: int n = randomInteger(low, high); * --------------------------------------- * returns a random integer in the range low to high, inclusive. */ int randomInteger(int low, int high); /* * Function: randomReal * Usage: double d = randomReal(low, high) * --------------------------------------- * Returns a random real number in the half-open interval [low, high). A * half-open interval includes the first endpoint but not the second, which * means that the result is always greater than or equal to low but * strictly less than high. */ double randomReal(double low, double high); /* * Function: randomChance * Usage: if(randomChance(p)) ... * ------------------------------- * Returns true with the probability indicated by p. The argument p must * be a floating-point number between 0 (never) and 1 (always). For * example, calling randomChance(.30) returns true 30 percent of time. */ bool randomChance(double p); /* * Function: setRandomSeed * Usage: setRandomSeed(seed); * --------------------------- * Sets the internal random number seed to the specified value. You can * use this function to set a specific starting point for the pseudorandom * sequence or to ensure that program behavior is repeatable during the * debugging phase. */ void setRandomSeed(int seed); #endif,c++,random,header,C++,Random,Header,以及源文件本身: #include <iostream> #include "random.h" using namespace std; /*Function prototypes*/ bool tryToMakePoint(int point); int rollTwoDice(); /*Main program*/ int main(){ cout << "This program plays a game of craps." << end
#include <iostream>
#include "random.h"
using namespace std;
/*Function prototypes*/
bool tryToMakePoint(int point);
int rollTwoDice();
/*Main program*/
int main(){
cout << "This program plays a game of craps." << endl;
int point = rollTwoDice();
switch (point){
case 7: case 11:
cout << "That's a natural. You win." <<endl;
break;
case 2: case 3: case 12:
cout << "That's craps. You lose." << endl;
break;
default:
cout << "Your point is " << point << "." << endl;
if (tryToMakePoint(point)){
cout << "You made your point. You win." <<endl;
} else {
cout << "You rolled a seven. You lose." << endl;
}
}
return 0;
}
bool tryToMakePoint(int point){
while(true){
int total = rollTwoDice();
if (total == point) return true;
if (total == 7) return false;
}
}
int rollTwoDice(){
cout << "Rolling the dice ..." <<endl;
int d1 = randomInteger(1, 6);
int d2 = randomInteger(1, 6);
int total = d1 + d2;
cout << "You rolled " << d1 << " and " << d2 << " - that's " << total << endl;
return total;
}
#包括
#包括“random.h”
使用名称空间std;
/*功能原型*/
布尔tryToMakePoint(整数点);
int rollTwoDice();
/*主程序*/
int main(){
您是否有函数的声明
int randomInteger(int, int);
但是,它没有定义,另一种说法是,当你调用它时,它不知道在功能上应该做什么。你需要这样的东西:
int randomInteger(int low, int high) {
return 4; // a dice was rolled to determine a random value
}
您有一个函数的声明
int randomInteger(int, int);
但是,它没有定义,另一种说法是,当你调用它时,它不知道在功能上应该做什么。你需要这样的东西:
int randomInteger(int low, int high) {
return 4; // a dice was rolled to determine a random value
}
您有一个函数的声明
int randomInteger(int, int);
但是,它没有定义,另一种说法是,当你调用它时,它不知道在功能上应该做什么。你需要这样的东西:
int randomInteger(int low, int high) {
return 4; // a dice was rolled to determine a random value
}
您有一个函数的声明
int randomInteger(int, int);
但是,它没有定义,另一种说法是,当你调用它时,它不知道在功能上应该做什么。你需要这样的东西:
int randomInteger(int low, int high) {
return 4; // a dice was rolled to determine a random value
}
您从未链接过该函数的定义。从技术上讲,这不是编译器错误,而是链接器错误。您从未链接过该函数的定义。从技术上讲,这不是编译器错误,而是链接器错误。您从未链接过该函数的定义。从技术上讲,这不是编译器错误,而是链接器错误。您从未链接过t的定义hat函数。从技术上讲,这不是编译器错误,而是链接器错误。好的,但通常在*.h文件中只有没有主体的函数原型?这里,在random.cpp代码的同一章中,所有这些函数都定义了:好的,但通常在*.h文件中只有没有主体的函数原型?这里,在代码的同一章中random.cpp,其中定义了所有这些函数:Ok,但通常在*.h文件中只是没有主体的函数原型?这里,在random.cpp代码的同一章中,定义了所有这些函数:Ok,但通常在*.h文件中只是没有主体的函数原型?这里,在random.cpp代码的同一章中,所有is功能定义如下: