私はすでにこれを理解しようとして時間を無駄にしましたが、今のところ運がありません...同じ問題についてStackOverflowを調べてみましたが、ほとんどはVSやEclipseなどの人々が使用しているIDEに関連しています。
スタンフォードリーダーのC++コースでいくつかの例を実行していましたが、コードが正しく機能しません。外部ライブラリの使用方法を理解しようとしていますが、問題が発生し続けます。おそらく正しいコマンドを使用していませんが、どのコマンドを使用すればよいかわかりません。
私はCygwinを使用しており、C++の演習を行うためのターミナルです。同じフォルダにすべてのファイルがあります。私はWindows7を使用していますが、それが最大の問題ではないはずです。
例として、このエラーは、g++Craps.cppを記述したときに得られるものをよく示しています。
$ g ++ Craps.cpp
/tmp/ccdTdi0t.o:Craps.cpp:(.text+0x1cf): `randomInteger(int、int)'への未定義の参照
/tmp/ccdTdi0t.o:Craps.cpp:(.text+0x1e6): `randomInteger(int、int)'への未定義の参照
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:/tmp/ccdTdi0t.o:bad
セクション`.ctors'のアドレス0x0を再配置します
collect2:ldが1つの終了ステータスを返しました
今回実行した例は、外部ライブラリを使用した例の1つにすぎず、他の例を間違えると同じ種類のエラーが発生します。ライブラリが見つかりません。
これは、Craps.cppとも呼ばれる私のメインです。
#include <iostream>
#include "random.h"
using namespace std;
bool tryToMakePoint(int point);
int rollTwoDice();
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.cpp:
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "random.h"
using namespace std;
void initRandomSeed();
int randomInteger(int low, int high) {
initRandomSeed();
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (double(high) - low + 1);
return int(floor(low + s ));
}
double randomReal(double low, double high) {
initRandomSeed();
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (high - low);
return low + s;
}
bool randomChance(double p) {
initRandomSeed();
return randomReal(0, 1) < p;
}
void setRandomSeed(int seed) {
initRandomSeed();
srand(seed);
}
void initRandomSeed() {
static bool initialized = false;
if (!initialized) {
srand(int(time(NULL)));
initialized = true;
}
}
そして最後に私のrandom.h:
#ifndef _random_h
#define _random_h
int randomInteger(int low, int high);
double randomReal(double low, double high);
bool randomChance(double p);
void setRandomSeed(int seed);
#endif
誰かが私を助けてくれることを願っています。間違っているのがCygwinコマンドである場合、私が何を書くべきかを見ることができれば素晴らしいでしょう。
編集:ちょうど私が本の中で例を正しく書き留めることさえできなかったことを知りました。今修正され、間違いがないはずです...私は心からそう願っています。申し訳ありません。