-3

I can not figure out what I have messed up on. I think I have some values messed up and I am trying to fix this. I feel like this is a simple array with my code but I am trying to sort this file and I'm getting this error.

1>ClCompile:
1>  Main.cpp
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl sort(struct salesTran * const,int)" (?sort@@YAXQAUsalesTran@@H@Z) referenced in function _main
1>C:\Users\BranN3W\Documents\Visual Studio 2010\Projects\3-1Notes\Debug\3-1Notes.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.96

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct salesTran {
    string name;
    double quantity,price;
};

void swap(salesTran A[], int i, int j);
void sort(salesTran A[], int size);

ostream& operator << (ostream& os, salesTran A)
{os << A.name << "\t" << A.quantity << "\t" << A.price;
    return os;}
istream& operator >> (istream& is, salesTran& A)
{is >> A.name >> A.quantity >> A.price;
    return is;}

int main()
{
    salesTran data[250];

    ifstream fin;
    fin.open("sales.txt");
    ofstream fout;
    fout.open("results.txt");

    int index = 0;
    fin >> data[index];
    while(!fin.eof())
    {
        index++;
        fin >> data[index];
    }

    sort(data, index);

    for(int j=0; j < index; j++)
    {
        cout << data[j] << endl;
    }

    return 0;
}

void swap(int data[], int i, int j)
{
    int temp;
    temp = data[i];
    data[i] = data[j];
    data[j] = temp;
    return;
}

void sort(int data[], int size)
{
    for(int p=1; p<size; p++)
    {
        for(int c=0; c<size-p; c++)
        {
            if(data[c]>data[c+1]) swap(data,c,c+1);
        }
    }
    return;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2133925
  • 377
  • 2
  • 4
  • 15
  • Can anyone point me in the right direction here? I could really use the help. I want to figure this out and its killing me. I've googled and checked all my books, but I can't get this to run. – user2133925 Mar 05 '13 at 01:33
  • Perhaps **defining** `void sort(salesTran A[], int size);` rather than just *declaring* it as a prototype would help. Your sort() routine at the bottom of the list is `void sort(int data[], int size)`. Look at the parameter lists. – WhozCraig Mar 05 '13 at 01:33
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?: Declared and undefined variable or function.](http://stackoverflow.com/a/12574403/902497) – Raymond Chen Mar 05 '13 at 01:41
  • How do I not declare it as a prototype? – user2133925 Mar 05 '13 at 01:42
  • You declare it as a prototype *and* implement it. Right now, you did the first step but not the second. – Raymond Chen Mar 05 '13 at 01:42
  • So should my swap function be like this: int temp; temp = int [i]; int[j] = int[j]; int[j] = temp; return; – user2133925 Mar 05 '13 at 02:02
  • I'm unaware what I should change the Ints to be. I would assume that it would just be int temp; temp =A[i]; A[j] = A[j]; A[j] = temp; return; or does that int temp need to be changed to salesTran? – user2133925 Mar 05 '13 at 02:17
  • @WhozCraig I need help with my changing my swap and sort functions to work with my matching parameters. – user2133925 Mar 05 '13 at 02:29

2 Answers2

1

It is because your sort function takes an int data[] parameter but when you use it you are passing it salestran data[]. You have a prototype for the void sort(salesTran A[], int size); but it is never defined. You will also need to change your sort function to use the struct properly.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Okay think you for the hints guys. I'm going to get try to fix these arrays. This may be a dumb question, But I thought I did define it? Can you point me in the right direction? Thank you! – user2133925 Mar 05 '13 at 01:40
  • @user2133925 You defined two different functions, one for the struct (the one at the top also called a prototype) and one for an `int []` (the actual definition) You have a linking error because the one for the struct is used but never defined. You need to either add a new function `void sort(int data[], int size) {` (and swap) and fill it out, or change the old `int []` function to match the functions that use the struct. – Fantastic Mr Fox Mar 05 '13 at 01:43
  • okay. So i need to change the old int[] at the bottom. I understand that it should be salesTran[] or index[]? Or am I way off? – user2133925 Mar 05 '13 at 01:49
  • @user2133925 If you want to go that way you will need to change the bottom functions to `void sort(salesTran data[], int size) {` you will also need to change how the function works by changing all the compares to whatever it is in the struct that you want to sort by. You will also need to do this for swap. – Fantastic Mr Fox Mar 05 '13 at 01:52
  • I just tried adding salestran A[] and index[]. It says the type is not allowed. I tried doing this at first. That is why i tried using INT, because it didn't give me an error until a tried to run the code. – user2133925 Mar 05 '13 at 01:52
  • Thank you for all your help so far. I feel like I am so close to getting this to run. My best guess for changing the function is So should my swap function be like this: int temp; temp = int [i]; int[j] = int[j]; int[j] = temp; return; .... This isn't working so obviously I am doing it wrong. Are the ints okay or do they need to go? – user2133925 Mar 05 '13 at 02:27
  • @ Ben I need help with my changing my swap and sort functions to work with my matching parameters. – user2133925 Mar 05 '13 at 02:28
  • @user2133925 Maybe try to do it yourself, if you have trouble, ask a new stack overflow question. – Fantastic Mr Fox Mar 05 '13 at 02:29
  • Okay. Thank you for your help. I'm sorry I bothered you. I didn't mean to nag – user2133925 Mar 05 '13 at 02:31
0

First, you declare your sort and swap functions like so:

void swap(salesTran A[], int i, int j);
void sort(salesTran A[], int size);

After which you define the functions as taking an array of integers instead of an array of salesTran structures.

void swap(int data[], int i, int j);
void sort(int data[], int size);

Your declarations and definitions must match for your code to link.

Eddy Luten
  • 432
  • 2
  • 11
  • 1
    Syntax error?? The code compiles fine. It doesn't **link** because he's calling a function declared but never defined. – WhozCraig Mar 05 '13 at 01:39
  • This is not the only thing the OP will need to change. The code in swap and sort is for an `int []` so these changes alone will not work. – Fantastic Mr Fox Mar 05 '13 at 01:40
  • Okay, so I changed my calls to match up. My actual swap function would look something like this? int temp; temp = int [i]; int[j] = int[j]; int[j] = temp; return; – user2133925 Mar 05 '13 at 02:01