g++编译时错误 undefined reference... error: ld returned 1 exit status

yuanoung
  • 10

g++编译时,错误如下

---------------------$ g++ hdu-1002.cpp -o exe-script/hdu-1002
/tmp/ccKSrfB8.o: In function `main':
hdu-1002.cpp:(.text+0x12f): undefined reference to `addBigNumber(std::vector<char, std::allocator<char> >, std::vector<char, std::allocator<char> >, std::vector<char, std::allocator<char> >, std::reverse_iterator<__gnu_cxx::__normal_iterator<char*, std::vector<char, std::allocator<char> > > >&, std::reverse_iterator<__gnu_cxx::__normal_iterator<char*, std::vector<char, std::allocator<char> > > >&)'
collect2: error: ld returned 1 exit status

g++ version:

---------$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.2.0-8ubuntu3.2' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3.2) 

代码

#include <iostream>
#include <vector>
#include <cctype>
using namespace std;

typedef vector<char> (vchar);
typedef vector<char>::iterator (vchar_it);
typedef vector<char>::reverse_iterator (vchar_rit);
void input(vchar& s);
void print_vector(vchar& s, int reverse);
void addRemainderNumber(vchar s, vchar& de, vchar_rit rit, int& incr);
int addBigNumber(vchar cNumA, vchar cNumB, vchar resultNum, vchar_rit& pa, vchar_rit& pb);

int main()  {
    int input_count(0);

    cin >> input_count;
    cin.get();
    for(int count = 1; count <= input_count; count++) {
        vchar cNumA, cNumB, resultNum;

        input(cNumA);
        input(cNumB);
        vchar_rit pa, pb;
        int incr = addBigNumber(resultNum, cNumA, cNumB, pa, pb);
        addRemainderNumber(cNumA, resultNum, pa, incr);
        addRemainderNumber(cNumB, resultNum, pb, incr);

        if (incr) resultNum.push_back(char(incr + 48));


        cout << "Case " << count << ":\n";
        print_vector(cNumA, 0);
        cout << " + ";
        print_vector(cNumB, 0);
        cout << " = ";
        print_vector(resultNum, 1);
        
        cout << endl;
        if (count != input_count)
            cout << endl;
    }
}

void addRemainderNumber(vchar s, vchar& de, vchar_rit rit, int& incr) {
    int r(0);
    while (rit != s.rend()) {
        if (incr == 0)
            de.push_back(*rit);
        else {
            r = int(*rit) - 48 + incr;
            incr = 0;
            if (r > 9) {
                incr = r / 10;
                r = r % 10;
            }
            de.push_back(char(r + 48));
        }
        ++rit;
    }
}
int addBigNumber(vchar& resultNum, vchar cNumA, vchar cNumB, vchar_rit& pa, vchar_rit& pb) {
    int r(0), incr(0);
    for (pa = cNumA.rbegin(), pb = cNumB.rbegin(); 
            pa != cNumA.rend() && pb != cNumB.rend(); pa++, pb++) {
        r = int(*pa) + int(*pb) + incr - 96;
        incr = 0;
        if (r > 9) {
            incr  = r / 10;
            r = r % 10;
        }
        resultNum.push_back(char(r + 48));
    }

    return incr;
}

void print_vector(vchar& s, int reverse) {
    int zero(1);
    if (reverse) {
        vchar_rit rit;
        for (rit = s.rbegin(); rit != s.rend(); rit++) {
            if ( zero && (*rit) == '0')
                continue;
            else {
                cout << (*rit);
                zero = 0;
            }
        }

        if (zero) cout << '0';

    } else {
        vchar_it it;
        for (it = s.begin(); it != s.end(); it++) {
            if ( zero && (*it) == '0')
                continue;
            else {
                cout << (*it);
                zero = 0;
            }
        }

        if (zero) cout << '0';

    }
}

void input(vchar& s) {
    char next;
    cin.get(next);

    while( !isspace(next)) {
        s.push_back(next);
        cin.get(next);
    }
}
回复
阅读 6.5k
2 个回答
✓ 已被采纳

int addBigNumber(vchar& cNumA, vchar cNumB, vchar resultNum, vchar_rit& pa, vchar_rit& pb);

老豆的咸可乐
  • 2
新手上路,请多包涵

addBigNumber 的声明和实现不一致吧?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏