开发者

return a list<int> from a function c++

Every time I try to use my add function and return a list from it. I get an undefined symbol error. What am I doing wrong here.

this is the error: Undefined first referenced symbol in file

add(std::list<int, std::allocator<int> >开发者_运维问答; const&, std::list<int, std::allocator<int> >)/var/tmp//cc78hUrW.o

ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status

#include <iostream>
#include <list>
#include <math.h>

using namespace std;

list<int> add(const list<int> &lhs, const list<int> $rhs);
list<int> sub(const list<int> &lhs, const list<int> $rhs);
list<int> mul(const list<int> &lhs, const list<int> &rhs);

int main(int argc, char* argv[])
{
    /*variables*/
    list<int> num1, num2, num3;
    list<int> ::iterator it1, it2, it3;
    char temp[1001];
    int x = 0, y = 0;

    it1 = num1.begin();

    for(x = 0; x <= 1001; x++)
        temp[x] = -42;

    cout << "Number 1: ";
    cin >> temp;

    for(x = 0; temp[x] != -42; x++)
        num1.insert(it1, temp[x] - '0');

    for(x = 0; x <= 1001; x++)
        temp[x] = -42;

    cout << "Number 2: ";
    cin >> temp;

    it2 = num2.begin();

    for(x = 0; temp[x] != -42; x++)
        num2.insert(it2, temp[x] - '0');

    it1 = num1.end();
    it1--;
    num1.erase(it1);

    for(it1 = num1.begin(); it1 != num1.end(); it1++)
         cout << *it1;
    cout << endl;

    it2 = num2.end();
    it2--;
    num2.erase(it2);

    for(it2 = num2.begin(); it2 != num2.end(); it2++)
    cout << *it2;
    cout << endl;

    num3 = add(num1, num2);

    for(it3 = num3.begin(); it3 != num3.end(); it3++)
    cout << *it3;
    cout << endl;

    return 0;
    }

    list<int> add(const list<int> &lhs,const list<int> &rhs)
    {
    /*Variables*/
    list<int> left = lhs;
    list<int> right = rhs;
    list<int> answer;
    list<int>::iterator itl, itr, ans;
    int cary = 0;
    int sum = 0;
    int lfint = 0, rtint = 0;
    int lsize=0, rsize=0;

    lsize = (int)left.size();
    rsize = (int)right.size();

    while(lsize < rsize)
    {
        itl = left.end();
        left.insert(itl, 0);
        lsize = (int)left.size();
    }

    while(rsize < lsize)
    {
         itr = right.end();
         right.insert(itr, 0);
         rsize = (int)right.size();
    }

    itl = left.begin();
    itr = right.begin();
    ans = answer.begin();

    while(itl != left.end())
    {
         lfint = *itl;
         rtint = *itr;
         sum = lfint + rtint;

         sum = sum + cary;
         cary = 0;

         if(sum >= 10)
         {
             sum = sum - 10;
             cary = 1;
         }

         answer.insert(ans, sum);
         itl++;
         itr++;
    }
    if(cary == 1)
    {
        answer.insert(ans, 1);
    }

    return answer;
    }


You used a $ instead of a & when you declared add() here...

list<int> add(const list<int> &lhs, const list<int> $rhs);


The problem is a typo near the beginning of your code:

list<int> add(const list<int> &lhs, const list<int> $rhs);
list<int> sub(const list<int> &lhs, const list<int> $rhs);

Replace the $ with & to fix it.


$ symbol means nothing in C++. Probably a typo.

list<int> add(const list<int> &lhs, const list<int> $rhs);
list<int> sub(const list<int> &lhs, const list<int> $rhs);

should be

list<int> add(const list<int> &lhs, const list<int> &rhs);
list<int> sub(const list<int> &lhs, const list<int> &rhs);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜