0

我已经在所有图书馆玩了很长时间(可能是 2 个月)。

所有其他 Collection 类,如vector, stack, queue 都运行良好。

但是,当我使用词典库时。除了我使用 foreach 函数一个一个地提取单词外,所有事情都进行得很顺利。

有一个错误,我试图弄清楚如何解决它一整天。

Ld ./WordLadder.app/Contents/MacOS/WordLadder normal i386
    cd "/Users/leungtimothy/Desktop/106B assignment/Assignment2-xcode 3/WordLadder"
    /Developer/usr/bin/llvm-g++-4.2 -arch i386 "-L/Users/leungtimothy/Desktop/106B assignment/Assignment2-xcode 3/WordLadder" -LStanfordCPPLib "-L/Users/leungtimothy/Desktop/106B assignment/Assignment2-xcode 3/WordLadder/StanfordCPPLib" "-F/Users/leungtimothy/Desktop/106B assignment/Assignment2-xcode 3/WordLadder" -filelist /Users/leungtimothy/Library/Developer/Xcode/DerivedData/WordLadder-awuwtyghwhswlubvikhvzubfsmuw/Build/Intermediates/WordLadder.build/Debug/WordLadder.build/Objects-normal/i386/WordLadder.LinkFileList -framework Cocoa -framework Carbon -framework QuickTime -lStanfordCPPLib -o "/Users/leungtimothy/Desktop/106B assignment/Assignment2-xcode 3/WordLadder/./WordLadder.app/Contents/MacOS/WordLadder"

ld: bad codegen, pointer diff in _fe::Range::Range()to global weak symbol vtable for _fe::Rangefor architecture i386
Command /Developer/usr/bin/llvm-g++-4.2 failed with exit code 1

我知道它看起来有点乱,是使用 g++ 4.2 的问题吗?我已经下载了最新的 cs106b 库。

/*
 * File: lexicon.h
 * ---------------
 * This interface exports the <code>Lexicon</code> class, which is a
 * compact structure for storing a list of words.
 */

#ifndef _lexicon_h
#define _lexicon_h

#include <string>
#include "FOR.cpp"
#include "set.h"
#include "stack.h"

/*
 * Class: Lexicon
 * --------------
 * This class is used to represent a <i>lexicon,</i> or word list.
 * The main difference between a lexicon and a dictionary is that
 * a lexicon does not provide any mechanism for storing definitions;
 * the lexicon contains only words, with no associated information.
 * It is therefore similar to a set of strings, but with a more
 * space-efficient internal representation.  The <code>Lexicon</code>
 * class supports efficient lookup operations for words and prefixes.
 */

#include <cctype>

class Lexicon {

public:

/*
 * Constructor: Lexicon
 * Usage: Lexicon lex;
 *        Lexicon lex(filename);
 * -----------------------------
 * Initializes a new lexicon.  The default constructor creates an empty
 * lexicon.  The second form reads in the contents of the lexicon from
 * the specified data file.  The data file must be in one of two formats:
 * (1) a space-efficient precompiled binary format or (2) a text file
 * containing one word per line.  The Stanford library distribution
 * includes a binary lexicon file named <code>English.dat</code>
 * containing a list of words in English.  The standard code pattern
 * to initialize that lexicon looks like this:
 *
 *<pre>
 *    Lexicon english("English.dat");
 *</pre>
 */

   Lexicon();
   Lexicon(std::string filename);

/*
 * Destructor: ~Lexicon
 * Usage: (usually implicit)
 * -------------------------
 * The destructor deallocates any storage associated with the lexicon.
 */

   ~Lexicon();

/*
 * Method: size
 * Usage: int n = lex.size();
 * --------------------------
 * Returns the number of words contained in the lexicon.
 */

   int size() const;

/*
 * Method: isEmpty
 * Usage: if (lex.isEmpty()) . . .
 * -------------------------------
 * Returns <code>true</code> if the lexicon contains no words.
 */

   bool isEmpty() const;

/*
 * Method: clear
 * Usage: lex.clear();
 * -------------------
 * Removes all words from the lexicon.
 */

   void clear();

/*
 * Method: add
 * Usage: lex.add(word);
 * ---------------------
 * Adds the specified word to the lexicon.
 */

   void add(std::string word);

/*
 * Method: addWordsFromFile
 * Usage: lex.addWordsFromFile(filename);
 * --------------------------------------
 * Reads the file and adds all of its words to the lexicon.
 */

   void addWordsFromFile(std::string filename);

/*
 * Method: contains
 * Usage: if (lex.contains(word)) . . .
 * ------------------------------------
 * Returns <code>true</code> if <code>word</code> is contained in the
 * lexicon.  In the <code>Lexicon</code> class, the case of letters is
 * ignored, so "Zoo" is the same as "ZOO" or "zoo".
 */

   bool contains(std::string word) const;

/*
 * Method: containsPrefix
 * Usage: if (lex.containsPrefix(prefix)) . . .
 * --------------------------------------------
 * Returns true if any words in the lexicon begin with <code>prefix</code>.
 * Like <code>containsWord</code>, this method ignores the case of letters
 * so that "MO" is a prefix of "monkey" or "Monday".
 */

   bool containsPrefix(std::string prefix) const;

/*
 * Macro: foreach
 * Usage: foreach (string word in lexicon) . . .
 * ---------------------------------------------
 * Iterates over the words in the lexicon in alphabetical order.
 */

   /* The foreach macro is defined in foreach.h */

/*
 * Method: mapAll
 * Usage: lexicon.mapAll(fn);
 *        lexicon.mapAll(fn, data);
 * --------------------------------
 * Calls the specified function on each word in the lexicon.  The second
 * form of the call allows the client to pass a data value of any type
 * to the callback function.
 */

   void mapAll(void (*fn)(std::string value));

   template <typename ClientDataType>
   void mapAll(void (*fn)(std::string value, ClientDataType & data),
               ClientDataType & data);

#include "private/lexiconpriv.h"

};

#include "private/lexiconimpl.cpp"

#endif


/*
 * File: foreach.h
 * ---------------
 * This interface defines the <code>foreach</code> keyword, which is
 * used to simplify iteration.  All iterable classes import this
 * interface, so clients never need to do so explicitly.  This version
 * of the interface also supports C++ strings and arrays.
 */

#ifndef _foreach_h
#define _foreach_h

/*
 * Statement: foreach
 * Usage: foreach (type var in collection) { . . . }
 * -------------------------------------------------
 * The <code>foreach</code> statement steps through the elements in
 * a collection.  It works correctly with the collection classes in
 * both the Standard Template Library and the Stanford C++ libraries,
 * but can also be used with C++ strings and statically initialized
 * arrays.
 *
 * <p>The following code, for example, prints every element in the
 * string vector <code>lines</code>:
 *
 *<pre>
 *    foreach (string str in lines) {
 *       cout << str << endl;
 *    }
 *</pre>
 *
 * Similarly, the following function calculates the sum of the character
 * codes in a string:
 *
 *<pre>
 *    int sumCharacterCodes(string str) {
 *       int sum = 0;
 *       foreach (char ch in str) sum += ch;
 *       return sum;
 *    }
 *</pre>
 *
 * As a simplification when iterating over maps, the <code>foreach</code>
 * macro iterates through the keys rather than the key/value pairs.
 */

   /* The foreach and in macros are defined in the foreachpriv.h file */

    #include "private/foreachpriv.h"

    #endif


/*main program*/
#include <iostream>
#include "console.h"
#include "lexicon.h"
#include "queue.h"
#include "simpio.h"
#include "vector.h"

using namespace std;

int main() {
// [TODO: fill in the code]
    Lexicon eng("EnglishWords.dat");
    foreach(string word in eng)
    cout << word;
    return 0;
}
4

2 回答 2

0

简单的解决方法是包含“foreach.h”标头。看这里:foreach 在 C++ 中不被识别

于 2013-05-16T04:37:05.380 回答
0

尝试通过使用导入字符串库,然后尝试编译它

于 2012-07-06T19:41:23.597 回答