C++新手求助!! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
newton108

C++新手求助!!

  •  
  •   newton108 2016 年 2 月 16 日 2569 次点击
    这是一个创建于 3722 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一共 5 个文件在同一文件夹。
    - main.cpp
    - genlib.h
    - genlib.cpp
    - simpio.h
    - simpio.cpp

    可是编译的时候说找不到 GetInteger(),可是我明明在 simpio.cpp 里定义了啊。

    $ g++ main.cpp
    Undefined symbols for architecture x86_64:
    "GetInteger()", referenced from:
    _main in main-32d6b5.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    文件如下。

    /* * File: mian.cpp * -------------- */ #include <iostream> #include "genlib.h" #include "simpio.h" int main() { int total; total = GetInteger(); cout << "The total is " << total << "." << endl; return 0; } 
    #ifndef GENLIB_H_ #define GENLIB_H_ /*  * File: genlib.h  * Version: 1.0CPP  * Last modified on Wed Sep 18 13:41:31 PDT 2002 by jzelenski  * ----------------------------------------------------------  * This header file is indicated to be included in  * all the programs written for CS106B/X and provides a few  * common definitions. Note this header has a "using namespace std"  * clause. If a file includes this header, it can then use  * features from the std namespace without qualifying by scope.  */ #include <string> using namespace std; /*  * Function: Error  * Usage: Error(msg)  * ------------------  * Error outputs an error string to the cerr stream and  * then throws a string exception corresponding to the error.  */ void Error(string str); // /* // * Function macro: main // * -------------------- // * The purpose of this macro definition is to rename the student // * main to Main in order to allow a custom main defined in our // * libraries to configure the application before passing control // * back to the student program. // */ // #ifdef __APPLE__ // #define main Main // #endif #endif /*GENLIB_H_*/ 
    /*  * File: genlib.cpp  * Last modified September 2011 by Colin  * ----------------  * This file implements the genlib.h interface  * It is needed by programs using the Stanford CS106B libraries  * If the ErrorException is unhandled by student code, it will  * be caught by init.cpp code, which wraps around the student's  * main() function  */ #include "genlib.h" #include <cstdlib> #include <iostream> ErrorException::ErrorException(string m="unspecified custom error") : msg(m) { } ErrorException::~ErrorException() throw() {} const char* ErrorException::what() const throw() { return this->msg.c_str(); } void Error(string str) { ErrorException err(str); throw err; } 
    #ifndef SIMPIO_H_ #define SIMPIO_H_ /*  * File: simpio.h  * Version: 1.0CPP  * Last modified on Wed Sep 18 13:34:29 PDT 2002 by jzelenski  * ----------------------------------------------------------  * This interface provides access to a simple package of  * functions that simplify the reading of console input.  */ #include "genlib.h" /*  * Function: GetInteger  * Usage: n = GetInteger();  * ------------------------  * GetInteger reads a line of text from standard input and scans  * it as an integer. The integer value is returned. If an  * integer cannot be scanned or if more characters follow the  * number, the user is given a chance to rety.  */ int GetInteger(); /*  * Function: GetLong  * Usage: n = GetLong();  * ---------------------  * GetLong reads a line of text from standard input and scans  * it into a long integer. The long is returned. If the   * number cannot be scanned or if extra characters follow it,  * the user is given a chance to retry.  */ long GetLong(); /*  * Function: GetReal  * Usage: x = GetReal();  * ---------------------  * GetReal reads a line of text from standard input and scans  * it as a double. If the number cannot be scanned or if extra  * characters follow after the number ends, the user is given  * a chance to reenter the value.  */ double GetReal(); /*  * Function: GetLine  * Usage: s = GetLine();  * ---------------------  * GetLine reads a line of text from standard input and returns  * the line as a string. The newline character that terminates  * the input is not stored as part of the string that is returned.  */ string GetLine(); #endif /*SIMPIO_H_*/ 
    /* File : simpio.cpp  * Last modified September 2011 by Colin  *  * Based on the discussion in the CS106L Course Reader, chapter 3  * http://www.stanford.edu/class/cs106l/course_reader.html  */ #include <iostream> #include <sstream> #include "genlib.h" /*  * Function: GetLine  * Usage: s = GetLine();  */ string GetLine() { string result; getline(cin, result); if (cin.fail()) { result = ""; cin.clear(); } return result; } /*  * Function: GetInteger  * Usage: n = GetInteger();  */ int GetInteger() { while(true) { stringstream converter; converter << GetLine(); int result; if (converter >> result) { char remaining; if(converter >> remaining) cout << "Unexpected character: " << remaining << endl; else return result; } else cout << "Please enter an integer." << endl; } } /*  * Function: GetLong  * Usage: n = GetLong();  */ long GetLong() { while(true) { stringstream converter; converter << GetLine(); long result; if(converter >> result) { char remaining; if(converter >> remaining) cout << "Unexpected character: " << remaining << endl; else return result; } else cout << "Please enter an integer (long)." << endl; } } /*  * Function: GetReal  * Usage: x = GetReal();  */ double GetReal() { while(true) { stringstream converter; converter << GetLine(); double result; if (converter >> result) { char remaining; if (converter >> remaining) cout << "Unexpected character: " << remaining << endl; else return result; } else cout << "Please enter a floating-point number."; } } 
    3 条回复    2016-02-16 12:50:53 +08:00
    omengye
        1
    omengye  
       2016 年 2 月 16 日 via Android   1
    关键词: g++ 多个源文件
    newton108
        2
    newton108  
    OP
       2016 年 2 月 16 日
    @omengye 谢谢,已搞定了。
    matthewgao
        3
    matthewgao  
       2016 年 2 月 16 日   1
    原因是你只编译了 main.cpp ,但是没编译其他的源文件,所以在连接的时候找不到 getInteger
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     965 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 89ms UTC 18:21 PVG 02:21 LAX 11:21 JFK 14:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86