首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯坦福大学CS106B词典库foreach函数不起作用

斯坦福大学CS106B词典库foreach函数不起作用
EN

Stack Overflow用户
提问于 2012-06-25 12:06:08
回答 2查看 1.5K关注 0票数 0

我已经使用所有的库很长时间了(也许2个月)。

所有其他的集合类,如vectorstack,queue都能很好地工作。

然而,当我使用词典库的时候。所有的事情都很顺利,除了我使用foreach函数逐个提取单词的时候。

有一个错误,我花了一整天的时间试图解决这个问题。

代码语言:javascript
复制
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库。

代码语言:javascript
复制
/*
 * 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;
}
EN

回答 2

Stack Overflow用户

发布于 2012-07-07 03:41:24

尝试使用导入字符串lib,然后尝试对其进行编译

票数 0
EN

Stack Overflow用户

发布于 2013-05-16 12:37:05

简单的解决方法是包含"foreach.h“头文件。看看这里:foreach not recognized in C++

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11183413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档