我在C++中使用stanford当然是cs106b,而且我被卡住了,我似乎无法正确理解它。对于知道这种东西的人来说,这可能是一个非常简单的解决方法。 我有三个文件,一个main.cpp和一个randword.h和randword.cpp。在randword.h中,我有#include“simpio.h”,它是一个定义GetLine()的斯坦福库。我可以让GetLine()在main.cpp文件中工作,但是当我尝试编译时,我在randword.cpp中得到了“未定义的对'GetLine()'的引用”。不能让#include工作
我使用代码块,并使用“添加文件...”功能。
下面的代码为main.cpp中:
#include "randword.h"
/* Private function prototypes */
/* Main program */
randword rw;
int main() {
rw.initDictionary();
}
randword.h:
#ifndef RANDWORD_H_INCLUDED
#define RANDWORD_H_INCLUDED
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "simpio.h"
#include "strutils.h"
using namespace std;
class randword{
public:
void initDictionary();
string chooseRandomWord();
string strArray[];
private:
};
#endif // RANDWORD_H_INCLUDED
random.cpp:
#include "randword.h"
using namespace std;
void randword::initDictionary(){
string fileName;
ifstream infile;
fileName = GetLine();
infile.open(fileName.c_str());
if(infile.fail()) cout << "Couldn't read file.";
return;
}
string randword::chooseRandomWord(){
string st1;
return st1;
}
任何帮助,将不胜感激!我怀疑这个问题已经发布,但我找不到它。谢谢!
是'simpio.h'与这些文件在同一个目录下吗? –
您需要*链接*您的预建斯坦福图书馆,其中包含的功能。我不使用CodeBlocks,但应该有一个选项来添加一个库到编译 - 后面... – Nim
'#include'模式看起来是正确的,但是当你将目标文件链接在一起时,链接器不能找到'GetLine()'的*定义*,它将在课程提供的某个目标文件('simpio.o'?)或库('cs106b.so'?)中。如果您的课程材料没有描述构建过程,请咨询教师。 – Beta