2013-03-05 66 views
-3

我無法弄清楚我搞砸了什麼。我認爲我有一些價值觀混亂,我試圖解決這個問題。我覺得這是一個簡單的數組與我的代碼,但我想排序這個文件,我得到這個錯誤。按字母順序排列結構數組

1>ClCompile: 
1> Main.cpp 
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl sort(struct salesTran * const,int)" ([email protected]@[email protected]@[email protected]) 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 

==========生成:0成功,1失敗,0上最新,0已跳過==========

#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; 
} 
+0

任何人都可以在這裏指出正確的方向嗎?我真的可以使用幫助。我想弄明白這一點,並殺死我。我用Google搜索了所有的書,但是我無法運行。 – user2133925 2013-03-05 01:33:04

+0

也許**定義**'void sort(salesTran A [],int size);'而不僅僅是*將它聲明爲原型會有所幫助。你在列表底部的sort()例程是'void sort(int data [],int size)'。看看參數列表。 – WhozCraig 2013-03-05 01:33:08

+0

可能重複[什麼是未定義的引用/未解析的外部符號錯誤,以及如何解決它?:聲明和未定義的變量或函數。](http://stackoverflow.com/a/12574403/902497) – 2013-03-05 01:41:23

回答

1

這是因爲您的sort函數需要int data[]參數,但是當您使用它時,您將通過它salestran data[]。你有一個void sort(salesTran A[], int size);的原型,但它從來沒有定義。您還需要更改排序函數以正確使用結構。

+0

好吧,想你的提示球員。我將嘗試修復這些數組。 這可能是一個愚蠢的問題,但我認爲我確實定義了它?你能爲我指出正確的方向嗎?謝謝! – user2133925 2013-03-05 01:40:24

+0

@ user2133925您定義了兩個不同的函數,一個用於struct(頂部的函數也稱爲prototype),另一個用於int [](實際的定義)。您有一個鏈接錯誤,因爲該結構的函數是使用但從未定義。你需要添加一個新的函數'void sort(int data [],int size){'(和swap)並填充它,或者改變舊的'int []'函數以匹配使用該結構的函數。 – 2013-03-05 01:43:34

+0

好的。所以我需要在底部更改舊的int []。我知道它應該是salesTran []或index []?還是我離開? – user2133925 2013-03-05 01:49:40

0

首先,你宣佈你的排序和交換的功能,像這樣:

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

其在定義功能以整數來代替salesTran結構數組的數組。

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

您的聲明和定義必須與代碼鏈接相匹配。

+1

語法錯誤?代碼編譯得很好。它不會**鏈接**,因爲他正在調用已聲明但未定義的函數。 – WhozCraig 2013-03-05 01:39:18

+0

這不是OP需要改變的唯一東西。交換和排序中的代碼是用於'int []',因此這些更改本身不起作用。 – 2013-03-05 01:40:21

+0

好的,所以我改變了我的電話以匹配。我的實際交換功能看起來像這樣? int temp; temp = int [i]; int [j] = int [j]; int [j] = temp; return; – user2133925 2013-03-05 02:01:05