C++ 斯坦福CS106B词典库foreach函数不工作
我已经和所有的图书馆玩了很长一段时间,大概有两个月了 所有其他集合类,如vector、stack和queue,都能很好地工作 但是,当我使用词典库时。除了使用foreach函数一个接一个地提取单词外,所有的事情都进行得很顺利 有一个错误,我一整天都在想如何解决它C++ 斯坦福CS106B词典库foreach函数不工作,c++,debugging,C++,Debugging,我已经和所有的图书馆玩了很长一段时间,大概有两个月了 所有其他集合类,如vector、stack和queue,都能很好地工作 但是,当我使用词典库时。除了使用foreach函数一个接一个地提取单词外,所有的事情都进行得很顺利 有一个错误,我一整天都在想如何解决它 Ld ./WordLadder.app/Contents/MacOS/WordLadder normal i386 cd "/Users/leungtimothy/Desktop/106B assignment/Assignme
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;
}
*/
词汇;
Lexiconstd::字符串文件名;
/*
*析构函数:~词典
*用法:通常是隐式的
* -------------
*析构函数解除分配与词典关联的任何存储。
*/
~词汇;
/*
*方法:大小
*用法:int n=lex.size;
* -------------
*返回词典中包含的字数。
*/
整数大小常数;
/*
*方法:isEmpty
*用法:如果lex.isEmpty。
* ----------------
*如果词典不包含单词,则返回true。
*/
布尔是空常数;
/*
*方法:清除
*用法:lex.clear;
* ----------
*从词典中删除所有单词。
*/
空洞清晰;
/*
*方法:添加
*用法:lex.addword;
* -----------
*将指定的单词添加到词典中。
*/
void addstd::字符串字;
/*
*方法:addWordsFromFile
*用法:lex.addwordsfromfilefilefilename;
* -------------------
*读取文件并将其所有单词添加到词典中。
*/
void addWordsFromFilestd::字符串文件名;
/*
*方法:包含
*用法:如果lex.com。
* ------------------
*如果单词包含在
*词典。在词汇课上,字母的大小写是
*忽略,因此Zoo与Zoo或Zoo相同。
*/
bool containsstd::字符串单词常量;
/*
*方法:containsPrefix
*用法:如果lex.containsPrefixprefix。
* ----------------------
*如果词典中的任何单词以前缀开头,则返回true。
*与其他方法一样,此方法忽略字母的大小写
*所以MO是monkey或Monday的前缀。
*/
bool containsPrefixstd::字符串前缀常量;
/*
*宏:foreach
*用法:词典中的每个字符串单词。
* -----------------------
*按字母顺序对词典中的单词进行迭代。
*/
/*foreach宏在foreach.h中定义*/
/*
*方法:mapAll
*用法:lexicon.mapAllfn;
*lexicon.mapAllfn,数据;
* ----------------
*对词典中的每个单词调用指定的函数。第二
*调用的形式允许客户端传递任何类型的数据值
*调用回调函数。
*/
void mapAllvoid*fnstd::字符串值;
样板
void mapAllvoid*fnstd::字符串值、ClientDataType和数据、,
ClientDataType&data;
包括private/lexiconpriv.h
};
包括private/lexiconimpl.cpp
恩迪夫
/*
*文件:foreach.h
* --------
*此接口定义foreach关键字,即
*用于简化迭代。所有iterable类都导入此
*接口,所以客户端永远不需要显式地这样做。这个版本
*接口还支持C++字符串和数组。
*/
如果没有
为每个人定义
/*
*声明:foreach
*用法:集合{…}中的foreach类型变量
* -------------------------
*foreach语句逐步遍历中的元素
*收藏。它可以正确地与中的集合类一起工作
*标准模板库和斯坦福C++库,
*也可以与C++字符串一起使用,并静态初始化
*数组。
*
*例如,下面的代码打印
*字符串向量行:
*
* 尝试使用导入字符串库,然后尝试编译它。简单的修复方法是包含foreach.h头。请看这里:是否有其他类似于使用迭代器的方法与使用foreach的方法相同?其语法是什么?词典::迭代器itr=名称?你能在你的问题中添加相关代码吗?很难说没有它会是什么样子。