0

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.
4

1 回答 1

3

"StanfordCPPLib/console.h"我在网上找到的一个随机包含有趣的代码,例如:

#if CONSOLE_FLAG | GRAPHICS_FLAG

#define main main(int argc, char **argv) { \
extern int _mainFlags; \
_mainFlags = GRAPHICS_FLAG + CONSOLE_FLAG; \
return startupMain(argc, argv); \
} \
int Main

extern int startupMain(int argc, char **argv);

因此,您的main()函数显然实际上是一个Main()最终由某个支持函数调用的函数,startupMain()该函数位于您的讲师应提供的库中。

您需要链接到该库。你的作业或课程笔记应该有关于如何做以及图书馆来自哪里的说明。

于 2013-05-29T18:52:53.003 回答