简体   繁体   English

不能#include工作

[英]can't get #include to work

I'm doing the stanford course cs106b in C++, and I'm stuck and I can't seem to get it right. 我正在用C ++做stanford课程cs106b,而且我被困住了,我似乎无法做到正确。 This probably a very easy fix for someone who knows this kind of stuff. 对于知道这种东西的人来说,这可能是一个非常简单的解决方案。 I have three files, one main.cpp and a randword.h and randword.cpp. 我有三个文件,一个是main.cpp,另一个是randword.h和randword.cpp。 In randword.h I have #include "simpio.h" which is a stanford library where GetLine() is defined. 在randword.h中,我有#include“simpio.h”,这是一个stanford库,其中定义了GetLine()。 I can get GetLine() to work in the main.cpp file but when I try to compile I get "undefined reference to 'GetLine()'" in randword.cpp. 我可以让getLine()在main.cpp文件中工作但是当我尝试编译时,我在randword.cpp中得到“未定义的'GetLine()'”引用。

I use codeblocks and I have used the "Add files..." function. 我使用代码块,我使用了“添加文件...”功能。

Here's the code for main.cpp: 这是main.cpp的代码:

  #include "randword.h"

  /* Private function prototypes */

  /* Main program */
  randword rw;
  int main() {
  rw.initDictionary();


}

randword.h: 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: 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;

   }

Any help would be much appreciated! 任何帮助将非常感激! I suspect that this question was already posted, but I couldn't find it. 我怀疑这个问题已经发布了,但我找不到了。 Thanks! 谢谢!

try adding the library manually using code blocks 尝试使用代码块手动添加库

  • Open up your project 打开你的项目
  • Right click your project and select build options.. 右键单击您的项目并选择构建选项..
  • select debugger 选择调试器
  • Go to linker settings 转到链接器设置
  • Under Link Librarys click "add" 在Link Librarys下单击“添加”
  • Find your lib file, select it and keep as a relative path 找到您的lib文件,选择它并保持相对路径
  • Your project SHOULD run, if not reply here(as i explained something wrong) 你的项目应该运行,如果没有在这里回复(因为我解释错了)

randword.cpp does NOT have the library file needed to use GetLine , you may have included it inside your header file, but this does not carry over to randword.cpp. randword.cpp没有使用GetLine所需的库文件,您可能已将其包含在头文件中,但这不会延续到randword.cpp。 You need to include the library file just as you would in any other file in order to have access to it's functions. 您需要像在任何其他文件中一样包含库文件,以便能够访问它的功能。

//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