C++新手求助!!

2016-02-16 12:03:26 +08:00
 newton108

一共 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 retry.
 */

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.";
    }
}
2179 次点击
所在节点    编程
3 条回复
omengye
2016-02-16 12:39:12 +08:00
关键词: g++ 多个源文件
newton108
2016-02-16 12:46:56 +08:00
@omengye 谢谢,已搞定了。
matthewgao
2016-02-16 12:50:53 +08:00
原因是你只编译了 main.cpp ,但是没编译其他的源文件,所以在连接的时候找不到 getInteger

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/256802

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX