简体   繁体   中英

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. 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. In randword.h I have #include "simpio.h" which is a stanford library where GetLine() is defined. 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.

I use codeblocks and I have used the "Add files..." function.

Here's the code for 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;

   }

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"
  • Find your lib file, select it and keep as a relative path
  • 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. 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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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