Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ Linker issue


ghost's Avatar
0 0

well, finally almost done, but now I get this:

Linking console executable: bin/Debug/cashReg
Undefined symbols:
  "LinkList<Sale::item>::toArray()", referenced from:
      Sale::showOrder()     in cashReg.o
  "LinkList<Sale::item>::pushBack(Sale::item)", referenced from:
      Sale::addItem(double, bool)in cashReg.o
      Sale::coupon(double)in cashReg.o
  "LinkList<Sale::item>::~LinkList()", referenced from:
      Sale::~Sale() in main.o
  "LinkList<Sale::item>::length()", referenced from:
      Sale::showOrder()     in cashReg.o
  "LinkList<Sale::item>::LinkList()", referenced from:
      Sale::Sale()  in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I have no idea what to do here. and my code is, all in all, 520 lines long.

here's the functions that are referencing the link list member functions:

void Sale::showOrder(){
    int aLength=order->length();//here
    item* a=new item[aLength];
    a=order->toArray();//here
    for(int i=0;i<aLength;i++){
        if(a[i].Taxable)
            std::cout << "T";
        else
            std::cout << " ";
        std::cout << "\t";
        //std::cout << a[i].name;
        //std::cout << "\t";
        std::cout << a[i].price << std::endl;
    }
    delete a;
}

double Sale::addItem(double cost,bool hasTax)
{
    buffer.number=num;
    ++num;
    buffer.price=cost;
    buffer.Taxable=hasTax;
    order->pushBack(buffer);//here
    Subtotal+=cost;
    if(hasTax){
        taxableTotal+=cost;
        taxTotal=(taxableTotal*TAX)+.005;
    }
    if(isTaxExempt){
        taxTotal=0;
    }
    Total=(Subtotal+taxTotal);
    return cost;
}

double Sale::coupon(double amount)
{
    if(amount<=Subtotal){
        Subtotal-=amount;
        buffer.number=num;
        ++num;
        buffer.price=-amount;
        buffer.Taxable=0;
        order->pushBack(buffer);//here
        return Total=Subtotal+taxTotal;
    }
    else{
        std::cout << "Coupon may not exceed total." << std::endl;
        return -1;
    }
}

~Sale(){delete order;}//in header

Sale()
{
    Subtotal=0;
    taxTotal=0;
    Total=Subtotal+taxTotal;
    itemCount=0;
    paid=false;
    isTaxExempt=false;
    spent=0;
    num=1;
    order=new LinkList<item>();//here
}

Sale(const char* orderId){
    std::ifstream collect;
    num=1;
    order=new LinkList<item>();
    char* retrieve=strcat("orders/",orderId);
    collect.open(retrieve);
    std::string s;
    while(!collect.eof()){
        buffer.number=num;
        ++num;
        collect >> s;
        std::stringstream(s.c_str()) >> buffer.price;
        collect >> s;
        std::stringstream(s.c_str()) >> buffer.Taxable;
        order->pushBack(buffer);//and here
    }
collect.close();
}

any suggestions?


ghost's Avatar
0 0

Why did you start a new thread for this?

So your lists are of the type "Sale::item" yes? What exactly does that mean? What is an item? It looks like it must be an object that you defined, so shouldn't it be a list of just item objects?


ghost's Avatar
0 0

Apophis wrote: Why did you start a new thread for this?

sorry, I should have added on to old thread

Apophis wrote: So your lists are of the type "Sale::item" yes? What exactly does that mean? What is an item? It looks like it must be an object that you defined, so shouldn't it be a list of just item objects? an item is a struct defined in my class Sale. I've tried defining it as public and private, and I've defined it before my class, I tried specifying every member function of my linklist class as friends, so they could access it as private, I even tried putting the #include after its definition, nothing seems to works.


ghost's Avatar
0 0

Zkunxen wrote: [quote]Apophis wrote: Why did you start a new thread for this?

sorry, I should have added on to old thread

Apophis wrote: So your lists are of the type "Sale::item" yes? What exactly does that mean? What is an item? It looks like it must be an object that you defined, so shouldn't it be a list of just item objects? an item is a struct defined in my class Sale. I've tried defining it as public and private, and I've defined it before my class, I tried specifying every member function of my linklist class as friends, so they could access it as private, I even tried putting the #include after its definition, nothing seems to works.[/quote]

Anyone correct me if I'm wrong, but a friend cannot access private data. Friends and family can access protected data though.


ynori7's Avatar
Future Emperor of Earth
0 0

Recursacro wrote: Anyone correct me if I'm wrong, but a friend cannot access private data. Friends and family can access protected data though. No, I'm pretty sure the purpose of making a class a friend is so it can access data in your private fields.


ghost's Avatar
0 0

from http://www.cplusplus.com/doc/tutorial/inheritance/: In this example, we have declared CRectangle as a friend of CSquare so that CRectangle member functions could have access to the protected and private members of CSquare, more concretely to CSquare::side, which describes the side width of the square.

I'm completely lost here, I could post the code, but I don't think 520 lines of code would fit very well… and even with just the files that seem to be causing the issue, that's still the majority of the code… (I think it's all but the linklist.h file)


ynori7's Avatar
Future Emperor of Earth
0 0

Just out of curiosity, why are you using a linked list? This is a money/account related program right? Wouldn't a vector work better?


ghost's Avatar
0 0

it would, but I wrote the linked list class myself, and I wanted to use it. also, if the order gets really large (probably larger than would be likely) wouldn't the program start to become slow with vectors, because it has to rewrite the entire internal array, and delete the original with every pushback, and at the same time, I don't want to allocate enough memory for 50 items for an order containing three. that's horribly inefficient. and what about removing an arbitrary item from the order? that would require rewriting the entire vector, whereas with a linked list I can remove an item from anywhere in the order with a single class method.