2014-02-11 66 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