2014-02-11 56 views
0

我正在通过程序http://stanford.edu/class/archive/cs/cs106b/cs106b.1142/lectures.shtml学习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 

和源文件本身:

#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; 
} 

他们都是在同一个目录。 编译时我得到:

[email protected]:~/Documents/CS106b> g++ Craps.cpp /tmp/ccWyN2aR.o: In function rollTwoDice()': Craps.cpp:(.text+0x165): undefined reference to randomInteger(int, int)' Craps.cpp:(.text+0x177): undefined reference to `randomInteger(int, int)' collect2: error: ld returned 1 exit status

怎么了?

+2

你从来没有链接过该函数的定义。 – PlasmaHH

+0

从技术上讲,这不是一个编译器错误,而是一个*链接器*错误。 –

回答

3

你有功能的声明

int randomInteger(int, int); 

然而,它没有定义,投入的另一种方式是它不知道,当你把它做什么功能。你需要这样的东西:

int randomInteger(int low, int high) { 
    return 4; // a dice was rolled to determine a random value 
} 
+0

好吧,但通常在* .h文件只是功能原型没有身体? – Tsiskreli

+0

在这里,在所有这些函数都被定义的random.cpp代码的同一章节中: – Tsiskreli