http://www.stanford.edu/class/cs106b/assignments/Assignment1-linux.zip
我正在为即将到来的 Coursera 课程自学这项作业。我修改了 0-Warmup 文件夹中的 Warmup.cpp 如下:
#include <iostream>
#include <string>
#include "StanfordCPPLib/console.h"
using namespace std;
/* Constants */
const int HASH_SEED = 5381; /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33; /* Multiplier for each cycle */
const int HASH_MASK = unsigned(-1) >> 1; /* All 1 bits except the sign */
/* Function prototypes */
int hashCode(string key);
/* Main program to test the hash function */
int main() {
string name;
cout << "Please enter your name: ";
getline(cin, name);
int code = hashCode(name);
cout << "The hash code for your name is " << code << "." << endl;
return 0;
}
int hashCode(string str) {
unsigned hash = HASH_SEED;
int nchars = str.length();
for (int i = 0; i < nchars; i++) {
hash = HASH_MULTIPLIER * hash + str[i];
}
return (hash & HASH_MASK);
}
它给了我这个错误:
andre@ubuntu-Andre:~/Working/Assignment1-linux/0-Warmup$ g++ Warmup.cpp -o a
/tmp/ccawOOKW.o: In function `main':
Warmup.cpp:(.text+0xb): undefined reference to `_mainFlags'
Warmup.cpp:(.text+0x21): undefined reference to `startupMain(int, char**)'
collect2: ld returned 1 exit status
这里有什么问题?
更新:现在让它工作。
1. cd to the folder containing assignment.cpp
2. g++ assignment.cpp StanfordCPPLib/*.cpp -o a -lpthread
StanfordCPPLib/*.cpp this part indicate that everything in the library will be compiled,
-pthread will link pthread.h, which is used by several utilities in the Stanford library.