繁体   English   中英

不能#include工作

[英]can't get #include to work

我正在用C ++做stanford课程cs106b,而且我被困住了,我似乎无法做到正确。 对于知道这种东西的人来说,这可能是一个非常简单的解决方案。 我有三个文件,一个是main.cpp,另一个是randword.h和randword.cpp。 在randword.h中,我有#include“simpio.h”,这是一个stanford库,其中定义了GetLine()。 我可以让getLine()在main.cpp文件中工作但是当我尝试编译时,我在randword.cpp中得到“未定义的'GetLine()'”引用。

我使用代码块,我使用了“添加文件...”功能。

这是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;

   }

任何帮助将非常感激! 我怀疑这个问题已经发布了,但我找不到了。 谢谢!

尝试使用代码块手动添加库

  • 打开你的项目
  • 右键单击您的项目并选择构建选项..
  • 选择调试器
  • 转到链接器设置
  • 在Link Librarys下单击“添加”
  • 找到您的lib文件,选择它并保持相对路径
  • 你的项目应该运行,如果没有在这里回复(因为我解释错了)

randword.cpp没有使用GetLine所需的库文件,您可能已将其包含在头文件中,但这不会延续到randword.cpp。 您需要像在任何其他文件中一样包含库文件,以便能够访问它的功能。

//randword.cpp
#include <iostream>
#include "simpio.h" //include again!!!

//code here....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM