2013-05-06 2 views
0

나는 C++ 프로젝트에서 일하고 있는데, 전에는 헤더 파일을 사용 해본 적이 없지만 프로젝트를 만들려고하고 있지만 링커 오류가 여러 번 발생했습니다. 그들을 고치 러 가려고!LNK 1120 + LNK2019 오류

오류 4 오류 LNK1120 : 3 개 확인되지 않은 외부의 C : \ 사용자 \ 스티븐 \ 다운로드 \ 08227_ACW2_TestHarness_12-13 \ 08227_ACW2_TestHarness_12-13 디버그 \ \ 08227_ACW.exe 1 일 08227_ACW

다음과 같이

오류

오류 3 오류 LNK2019 : 확인할 수없는 외부 기호 "public: bool __thiscall ArrayStorage::exists(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"(? @ ArrayStorage @@ QAE_NV? $ basic_string @ DU? $ char_traits @ D @ std @@ V? $ allocator @ D @ 2 @@ std @@@ Z) 함수 _main에서 참조되는 C : \ Users \ Stephen \ Downloads \ 08227_ACW2_TestHarness_12-13 \ 08227_ACW2_TestHarness_12-13 \ main.obj 08227_ACW

오류 2 오류 LNK2019 : 해결되지 않은 외부 기호 "public: bool __thiscall ArrayStorage::stdExists(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"(? stdExists @ ArrayStorage @@ QAE_NV? $ basic_string @ DU? $ char_traits @ D @ std @@ V? $ allocator @D @ 2 @@ std @@@ Z) 함수 _main의 C에서 참조 : \ 사용자 \ 스티븐 \ 다운로드 \ 08227_ACW2_TestHarness_12-13 \ 08227_ACW2_TestHarness_12-13 \ 08227_ACW

오류 1 오류 LNK2019 main.obj : 확인되지 않은 외부 기호 "public: bool __thiscall LinkedListStorage::exists(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"(@ LinkedListStorage @@ QAE_NV 존재? $ basic_string @ DU? $ char_traits @ D @ std @@ V? $ allocator @ D @ 2 @@ std @@@ Z) _main 함수에서 참조되는 C : \ Users \ Stephen \ Downloads \ 08227_ACW2_TestHarness_12-13 \ 08227_ACW2_TestHarness_12-13 \ main.obj 08227_ACW

주위를 둘러보기 그들은 링커 오류 일종이지만, 나는 실제 문제가 무엇인지 전혀 모른다. 나는 내 코드를 올리는 것이 도움이 될지조차 모른다. 누군가가 나를 알리고 그것을 올려 놓을 수 있다면 필요할 지 모른다.

미리 감사드립니다.

편집 :

헤더 파일 :

ArrayStorage.h

#ifndef mao 
#define mao 

#include <fstream> 
#include <string> 
#include <iostream> 
#include <algorithm> 

using namespace std; 

class ArrayStorage 
{ 
private: 
    string* storageArray; 
    int aSize; 
public: 
    void read(ifstream& iFile); 
    void write(ofstream& oFile); 
    bool exists(string target); 
    bool stdExists(string target); 
friend ofstream& operator<<(ofstream& OS, ArrayStorage& SA); 
friend ifstream& operator>>(ifstream& IS, ArrayStorage& SA); 
}; 

#endif 

LinkedListStorage.h

#ifndef lao 
#define lao 

#include <fstream> 
#include <string> 
#include <iostream> 
#include <algorithm> 

using namespace std; 

class LinkedListStorage 
{ 
private: 
    void addnode(string line); 
    void sort(); 
    typedef struct node; 
public: 
    node *root; 
    int size; 
    void read(ifstream& iFile); 
    void write(ofstream& oFile); 
    bool exists(string target); 

friend ofstream& operator<<(ofstream& OS,LinkedListStorage& LLS); 
friend ifstream& operator>>(ifstream& IS, LinkedListStorage& LLS); 

}; 

#endif 

main.ccp

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

// ***************************** 
// you need to create these classes 
// ***************************** 
#include "ArrayStorage.h" 
#include "LinkedListStorage.h" 


int main(int argc, char **argv) { 
string find = "pixel"; 

// ###################################################### 
// #################### ArrayStorage #################### 
// ###################################################### 

// *********************************** 
//  sort read & exists 
// *********************************** 
ifstream fin1("ACW2_data.txt"); 
ofstream out1("1-In-SortedRead.txt"); 

if(!fin1.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

ArrayStorage arrayStorage1; 

// read in values into data structure 
arrayStorage1.read(fin1); 

// output values in data structure to file 
arrayStorage1.write(out1); 

fin1.close(); 
out1.close(); 

// find an item in the data structure using own search method 
if(arrayStorage1.exists(find)) { 
    cout << find.c_str() << " found exists()" << endl; 
} else { 
    cout << find.c_str() << " not found exists()" << endl; 
} 

// find an item in the data structure using std::count method 
if(arrayStorage1.stdExists(find)) { 
    cout << find.c_str() << " found stdExists()" << endl; 
} else { 
    cout << find.c_str() << " not found stdExists()" << endl; 
} 



// ********************************* 
// sort read & then copy constructor 
// ********************************* 
ifstream fin2("ACW2_data.txt"); 
ofstream out2("2-Out-CopyConstructor.txt"); 

if(!fin2.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

ArrayStorage arrayStorage2; 

// read in values into data structure 
arrayStorage2.read(fin2); 

ArrayStorage arrayStorage3 = arrayStorage2; 

// output values in data structure to a file 
arrayStorage3.write(out2); 

fin2.close(); 
out2.close(); 



// ************************************* 
// >> read & then << write 
// ************************************* 
ifstream fin3("ACW2_data.txt"); 
ofstream out3("3-In-OperatorRead.txt"); 
ofstream out4("4-Out-OperatorWrite.txt"); 

if(!fin3.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

ArrayStorage arrayStorage4; 

fin3 >> arrayStorage4; 
arrayStorage4.write(out3); 

out4 << arrayStorage4; 

fin3.close(); 
out3.close(); 
out4.close(); 



// ########################################################### 
// #################### LinkedListStorage #################### 
// ########################################################### 

// *********************************** 
//  sort read & exists 
// *********************************** 
ifstream fin4("ACW2_data.txt"); 
ofstream out5("5-In-SortedRead.txt"); 

if(!fin4.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

LinkedListStorage llStorage1; 

// read in values into data structure 
llStorage1.read(fin4); 

// output values in data structure to file 
llStorage1.write(out5); 

fin4.close(); 
out5.close(); 

// find an item in the data structure using own search method 
if(llStorage1.exists(find)) { 
    cout << find.c_str() << " found exists()" << endl; 
} else { 
    cout << find.c_str() << " not found exists()" << endl; 
} 


// ********************************* 
// sort read & then copy constructor 
// ********************************* 
ifstream fin5("ACW2_data.txt"); 
ofstream out6("6-Out-CopyConstructor.txt"); 

if(!fin5.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

LinkedListStorage llStorage2; 

// read in values into data structure 
llStorage2.read(fin5); 

LinkedListStorage llStorage3 = llStorage2; 

// output values in data structure to a file 
llStorage3.write(out6); 

fin5.close(); 
out6.close(); 


// ************************************* 
// >> read & then << write 
// ************************************* 
ifstream fin6("ACW2_data.txt"); 
ofstream out7("7-In-OperatorRead.txt"); 
ofstream out8("8-Out-OperatorWrite.txt"); 

if(!fin6.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

LinkedListStorage llStorage4; 

fin6 >> llStorage4; 
llStorage4.write(out7); 

out8 << llStorage4; 

fin6.close(); 
out7.close(); 
out8.close(); 

cout << endl << "Finished" << endl; 
int keypress; cin >> keypress; 
return 0; 
} 
그 중 하나가 필요하지 않은 경우 텍스트의 거대한 벽 617,451,515,

LinkedListStorage.ccp

#include <fstream> 
#include <string> 
#include <iostream> 
#include <algorithm> 

using namespace std; 

class LinkedListStorage 
{ 
private: 
//methods 

//variables 
typedef struct node 
{ 
    string word;// data 
    node *next; //address of next node 
}; 
node *root; //root node 
int size; //size of datafile 
    public: 
//methods 
void addnode(string line) 
{ 
    node *temp, *temp2; 
    temp = new node; 

    temp->word = line; 
    temp->next = NULL; 

    if(root == NULL) 
     root = temp; 
    else 
    { 
     temp2 = root; 
     while(temp2->next != NULL) 
      temp2 = temp2->next; 
     temp2->next = temp; 
    } 
} 
void sort()//simple bubblesort 
{ 
    node *temp, *temp2; 
    temp = new node; 
    temp2 = new node; 

    string spare = 0; 
    for(temp = root; temp!=NULL;temp = temp->next) 
    { 
     if(temp->word > temp2->word) 
     { 
      spare = temp->word; 
      temp->word = temp2->word; 
      temp2->word = spare; 
     } 
    } 

} 
void read(ifstream& iFile) 
{ 
    size = 0; 
    string line; 
    if(iFile.is_open()) 
    { 
     while(std::getline(iFile, line)) 
      ++size; //Figures out the size for the dynamic array 
    } 
    root = new node; 
    root->next = 0;//null 
    root->word = ""; //no data yet 

    for (int i = 0; i < size; i++) 
    { 
     if(i<3) 
      iFile.ignore(); 
     getline(iFile,line); 
     addnode(line); 
     sort(); 
    } 

} 
void write(ofstream& oFile) 
{ 
    node *temp; 
    temp = root; 
    while (temp!=NULL) 
    { 
     oFile << temp->word << endl; 
     temp = temp->next; 
    } 
} 
bool exists(string target) //I cant think of a single way to search a singly linked  list that is faster than O(n) 
{ 
    node *temp; 
    temp = root; 
    while (temp!=NULL) 
    { 
     if (temp->word == target) 
      return true; 
    } 
    return false; 
} 
//Constructor 
LinkedListStorage(); 
//Destructor 
~LinkedListStorage() 
{ 
    node *ptr; 

    for (ptr = root; root;ptr = root) 
    { 
     root = root->next; 
     delete ptr; 
    } 
} 

LinkedListStorage(const LinkedListStorage &other) :root(NULL) 
{ 
    node *cur = other.root; 
    node *end = NULL; 

    while(cur) 
    { 
     node* x = new node; 
     x->word = cur->word; 

     if(!root) 
     { 
      root = x; 
      end = root; 
     } 
     else 
     { 
      end->next = x; 
      end = x; 
     } 

     cur = cur->next; 
    } 
} 
friend ofstream& operator<<(ofstream& OS,LinkedListStorage& LLS); 
friend ifstream& operator>>(ifstream& IS, LinkedListStorage& LLS); 

}; 

ofstream& operator<<(ofstream& OS, LinkedListStorage& LLS) 
{ 
LLS.write(OS); 
return OS; 
} 

ifstream& operator>>(ifstream& IS, LinkedListStorage& LLS) 
{ 
LLS.read(IS); 
return IS; 
} 

and finally ArrayStorage.ccp 



#include <fstream> 
#include <string> 
#include <iostream> 
#include <algorithm> 

using namespace std; 


class ArrayStorage 
{ 
//Variables 
private: string* storageArray; 
    int aSize; // array size 
public: 
//methods 
void read(ifstream& iFile) 
{ 
    aSize = 0; //intialise 
    string line; 
    if(iFile.is_open()) 
    { 
     while(std::getline(iFile, line)) 
      ++aSize; //Figures out the size for the dynamic array 
    } 
    string *pnArray = new string[aSize];//intialise array 
    for (int i = 0; i < aSize; i++) 
    { 
     if(i<3) 
     { 
      iFile.ignore(); 
     } 
     getline(iFile,pnArray[i]); //this should not contain any important data due to the way sorting is done 
     sort(pnArray,pnArray + i); //sorts the array 
    } 
    storageArray = pnArray; 
} 
void write(ofstream& oFile) 
{ 
    if(oFile.is_open()) 
    { 
     for (int j = 0; j < aSize; j++) 
     { 
      oFile << storageArray[j] << endl; 
     } 
    } 
} 
bool exists(string target) 
{ 
    int lo = 1; 
    int hi = aSize - 1; 
    int mid = 0; 
    int comparitor = 0; 
    while(true) 
    { 
     mid =(lo+hi+1)/2; // the plus one is to force it to round up to the nearest highest integer 
     if(mid == hi) 
     { 
      if(comparitor = target.compare(storageArray[lo]) == 0) 
      { 
       return true; 
      } 
      else if(comparitor = target.compare(storageArray[hi]) == 0) 
      { 
       return true; 
      } 
      else 
      { 
      return false; 
      } 
     } 
     comparitor = target.compare(storageArray[mid]); 
     if(comparitor == 0) 
      return true; 
     else if(comparitor > 0) 
      lo = mid; 
     else if(comparitor < 0) 
      hi = mid;   
    } 
} 
bool stdExists(string target) 
{ 
    int check = count(storageArray,storageArray+(aSize-1),target); 
    if(check >0) 
     return true; 
    else 
     return false; 
} 
//copy constructor 
ArrayStorage(const ArrayStorage &other) 
{ 
    storageArray = new string[other.aSize]; 
    aSize = other.aSize; 
    memcpy(storageArray,other.storageArray,sizeof(string) * aSize); 
} 
//constructor 
ArrayStorage(); 
//Destructor 
~ArrayStorage() 
{ 
    delete [] storageArray; 
} 
//overload operator 
const ArrayStorage &operator=(const ArrayStorage &other) 
{ 
    if(this == &other) return *this; 
    delete[] storageArray; 
    storageArray = new string[other.aSize]; 
    aSize = other.aSize; 
    memcpy(storageArray,other.storageArray,sizeof(string) * aSize); 
    return *this; 

    //Friends for benefit 

} 
friend ofstream& operator<<(ofstream& OS, ArrayStorage& SA); 
friend ifstream& operator>>(ifstream& IS, ArrayStorage& SA); 
}; 

ofstream& operator<<(ofstream& OS, ArrayStorage& SA) 
{ 
SA.write(OS); 
return OS; 
} 

ifstream& operator>>(ifstream& IS, ArrayStorage& SA) 
{ 
SA.read(IS); 
return IS; 
} 

죄송합니다, 알려주세요 그리고 난 그것을 제거 드리겠습니다!

+0

'LinkedListStorage' 및'ArrayStorage'를 정의하는 링크 중 일부 파일이 링크되지 않았습니까? –

+0

'source' 파일의 멤버들 앞에서 헤더 파일 이름을'scope' 했습니까? – Mushy

답변

1

소스 파일이 각 클래스를 재정의하고 있습니다. 그들은 그렇게해서는 안됩니다. 대신 클래스를 정의하는 헤더를 포함하고 클래스에 선언 된 각 함수를 정의해야합니다. 예 :

// ArrayStorage.cpp 
#include "ArrayStorage.h" 

bool ArrayStorage::exists(string target) { 
    // Function body here 
} 
+0

얼마나 이상한가 ... 나는 그들을 구현했고, 내 헤더 파일 내에서 내가 믿어야한다고 선언했다. 코드를 추가하기 위해 내 질문을 편집해야 하는가? –

+0

@SteHawkins : 그렇습니다. 선언하고 구현 한 방법을 보여 주면 도움이 될 수 있습니다. –

+0

@SteHawkins : 코드를 게시 해 주셔서 감사합니다. 문제는 멤버 함수를 구현할 때 소스 파일의 전체 클래스를 다시 정의한다는 것입니다. –