2013-05-20 37 views
-3

它與主要的兩個功能做。 getinput和execfunc。它們被定義在Bibliotek類中。我搜索了這個。它可以是前向聲明的東西嗎?錯誤信息是:我編譯時得到這個錯誤:錯誤LNK2019:我讓他們兩個定義函數在主

1>------ Rebuild All started: Project: ou4sec, Configuration: Debug Win32 ------ 
1> ou4sec.cpp 
1> bibliotek.cpp 
1> Generating Code... 
1>ou4sec.obj : error LNK2019: unresolved external symbol "void __cdecl execfunc(class std::vector<class Lendobj *,class std::allocator<class Lendobj *> > &,char &,int &)" ([email protected]@[email protected]@@[email protected]@@@[email protected]@@std) referenced in function _main 
1>ou4sec.obj : error LNK2019: unresolved external symbol "char __cdecl getinput(void)" ([email protected]@YADXZ) referenced in function _main 
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol [email protected] referenced in function ___tmainCRTStartup 
1>c:\users\jonas\documents\visual studio 2010\Projects\ou4sec\Debug\ou4sec.exe : fatal error LNK1120: 3 unresolved externals 
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ========== 

文件(主)ou4sec.cpp:

#include "bok.h" 
#include "cd.h" 
#include "tidskrift.h" 
#include "fack.h" 
#include "nonfack.h" 
#include "lendobj.h" 
#include <iostream> 
#include <fstream> 
using namespace std; 
#include <string> 
#include <cstdio> 
#include <vector> 
#include <cstdlib> 


char getinput(); 
void execfunc(vector <Lendobj*> &bib,char &c, int &antal); 
int main() 
{ 
    char c; 
    int antal = 0; 
    vector <Lendobj*> biblio; 
    fstream inf; 
    inf.open("biblio.txt"); 
    std::string titel; 
    std::string forf; 
    int id; 
    int cnr; 
    std::string vol; 
    std::string art; 
    std::string typ; 
    string tid; 
    // If we couldn't open the input file stream for reading 
    if(inf.is_open()) 
    { 


     while (inf.good()) 
    { 
     // read stuff from the file into a string and print it 
     std::string strInput; 
     std::string typ; 
     int i = 0; 
     while(getline(inf, strInput)) 
      { 
     if(strInput == "Fiction") 
     { 
      typ = strInput; 
      getline(inf, strInput); 
      forf = strInput; 
      getline(inf, strInput); 
      titel = strInput; 
      getline(inf, strInput); 
      id = atoi (strInput.c_str()); 
      getline(inf, strInput); 
      cnr = atoi (strInput.c_str()); 
      biblio.push_back(new Nonfack(typ,id, titel, cnr, forf)); 
      i = i +1; 
     } 
     if(strInput == "NonFiction") 
     { 
      typ = strInput; 
      getline(inf, strInput); 
      forf = strInput; 
      getline(inf, strInput); 
      titel = strInput; 
      getline(inf, strInput); 
      id = atoi (strInput.c_str()); 
      getline(inf, strInput); 
      cnr = atoi (strInput.c_str()); 
      biblio.push_back(new Fack(typ,id, titel, cnr, forf)); 
      i = i +1; 
     } 
     if(strInput == "Journal") 
     { 
      typ = strInput; 
      getline(inf, strInput); 
      titel = strInput; 
      getline(inf, strInput); 
      vol = strInput; 
      getline(inf, strInput); 
      id = atoi (strInput.c_str()); 
      getline(inf, strInput); 
      cnr = atoi (strInput.c_str()); 
      biblio.push_back(new Tidskrift(typ,id, titel, cnr, vol)); 
      i = i +1; 
     } 
     if(strInput == "CD") 
     { 
      typ = strInput; 
      getline(inf, strInput); 
      art = strInput; 
      getline(inf, strInput); 
      titel = strInput; 
      getline(inf, strInput); 
      tid = strInput; 
      getline(inf, strInput); 
      id = atoi (strInput.c_str()); 
      getline(inf, strInput); 
      cnr = atoi (strInput.c_str()); 
      biblio.push_back(new Cd(typ,id, titel,cnr, art, tid)); 
      i = i +1; 
     } 
     } 
     antal = i; 
     inf.close(); 
    } 

    } 
    else cout << "Unable to open file"<<endl; 
    cout << "antal : " << antal << endl; 
    cout <<"Choose one of X/C/F/N/J/H(help)/S/B/R/Q: "<< endl; 
    while(1) 
    { 
     c = getinput(); 
     if(c=='Q') 
      break; 
     execfunc(biblio, c, antal); 
    } 
    remove("biblio.txt"); 
    for(int i = 0;i < antal;i++) 
    { 
     biblio[i]->quit(); 
    } 
    return 0; 
} 

文件 「bibliotek.h」:

#ifndef BIBLIOTEK_H 
#define BIBLIOTEK_H 
#include "lendobj.h" 
#include <vector> 

class Bibliotek 
{ 
public: 
    Bibliotek(); 
    static void help(); 
    static char getinput(); 
    static void insert(vector <Lendobj*> & bib,char &type, int &antal); 
    static void searchinit(vector <Lendobj*> & bib, int &antal); 
    static void removeinit(vector <Lendobj*> & bib, int &antal); 
    static void borrowinit(vector <Lendobj*> & bib, int &antal); 
    static void givebackinit(vector <Lendobj*> & bib, int &antal); 
    void execfunc(vector <Lendobj*> &bib,char &c, int &antal); 
    ~Bibliotek(); 
}; 
#endif 

文件 「bibliotek.cpp」:

#include "bibliotek.h" 
#include "bok.h" 
#include "cd.h" 
#include "tidskrift.h" 
#include "nonfack.h" 
#include "fack.h" 
#include "lendobj.h" 
#include <cstdio> 
#include <vector> 
#include <fstream> 
#include <istream> 
#include <string> 
#include <iostream> 
using namespace std; 

Bibliotek::Bibliotek(void) 
{ 
} 
Bibliotek::~Bibliotek(void) 
{ 
} 
void Bibliotek::help() 
{ 
    cout << " C - insert new CD" << endl; 
    cout << " F - insert new Fiction book" << endl; 
    cout << " N - insert new Non-fiction book"<< endl; 
    cout << " J - insert new Journal" << endl; 
    cout << " X - remove an object from the library" << endl; 
    cout << " H - show this text" << endl; 
    cout << " S - search" << endl; 
    cout << " B - borrow an object" << endl; 
    cout << " R - return an object" << endl; 
    cout << " Q - quit the program" << endl; 

} 

char Bibliotek::getinput() 
{ 
    char c; 
    while(c = getchar(),c!='C'&&c!='F'&&c!='N'&&c!='J'&& c!='X'&& 
     c!='H'&&c!='S'&&c!='B'&&c!='R'&&c!='Q') 
    { 
     cout <<"Choose one of X/C/F/N/J/H(help)/S/B/R/Q"<<endl; 
    } 
    return c; 
} 

void Bibliotek::insert(vector <Lendobj*> &bib,char &type, int &antal) 
{ 
    ostringstream os; 
    string artist; 
    string titel; 
    string speltid; 
    int lendid; 
    int cnr; 
    string author; 
    string volym; 
    cin.clear(); 
    cin.sync(); 
    if(type == 'C') 
    { 
     cout << "Enter Artist name: "; 
     getline(cin,artist,'\n'); 
     cout << "Enter title: "; 
     getline(cin,titel); 
     cout << "Enter playtime: "; 
     getline(cin,speltid); 
     lendid = bib[antal-1]->Getid()+1; 
     cnr = 0; 
     bib.push_back(new Cd("CD",lendid, titel, cnr, artist, speltid)); 
     antal = antal + 1; 
    } 
    if(type == 'F') 
    { 
     cout << "Enter name of Author : "; 
     getline(cin,author); 
     cout << "Enter title: "; 
     getline(cin,titel); 
     lendid = bib[antal-1]->Getid()+1; 
     cnr = 0; 
     bib.push_back(new Nonfack("Fiction",lendid, titel, cnr, author)); 
     antal = antal + 1; 
    } 
    if(type == 'N') 
    { 
     cout << "Enter name of Author : "; 
     getline(cin,author); 
     cout << "Enter title: "; 
     getline(cin,titel); 
     lendid = bib[antal-1]->Getid()+1; 
     cnr = 0; 
     bib.push_back(new Fack("NonFiction",lendid, titel, cnr, author)); 
     antal = antal + 1; 
    } 
    if(type == 'J') 
    { 
     cout << "Enter name of Journal : "; 
     getline(cin,titel); 
     cout << "Enter volume: "; 
     getline(cin,volym); 
     lendid = bib[antal-1]->Getid()+1; 
     cnr = 0; 
     bib.push_back(new Tidskrift("Journal",lendid, titel, cnr, volym)); 
     antal = antal + 1; 
    } 
    cout <<"Choose one of X/C/F/N/J/H(help)/S/B/R/Q"<<endl; 
} 
void Bibliotek::searchinit(vector <Lendobj*> & bib, int &antal) 
{ 
    char stype; 
    string sterm; 
    string result; 
    cin.clear(); 
    cin.sync(); 
    cout << "Search for title (T) or author/artist (A)?" << endl; 
    cin >> stype; 
    cout << "Enter search term: "; 
    cin >> sterm; 
    vector <Lendobj*>::const_iterator it; 
    for(it = bib.begin(); it != bib.end();it++) 
    { 
     if(stype == 'T') 
     result = (*it)->searcht(sterm); 
     if(!result.empty()) 
     { 
     (*it)->printres(result); 
     result = ""; 
     } 
     else if(stype == 'A') 
     result = (*it)->searcha(sterm); 
     if(!result.empty()) 
     { 
     (*it)->printres(result); 
     result = ""; 
     } 
    } 


} 
void Bibliotek::removeinit(vector <Lendobj*>& bib, int &antal) 
{ 
    int id; 
    int index = 0; 
    int found= 0; 
    cout << "antal: " << antal << endl; 
    cout << "Enter id of object to remove: " ; 
    cin >> id; 
    vector <Lendobj*>::iterator it; 
    for(it = bib.begin(); it != bib.end();it++) 
    { 
    if((*it)->Getid() == id) 
    { 
     delete *it; 
     it = bib.erase(it); 
     found = 1; 
     break; 
    } 
    } 

    antal = antal-1; 
    if (found == 0) 
     cout<<"ID does not exist!"<<endl; 
} 
void Bibliotek::borrowinit(vector <Lendobj*> & bib, int &antal) 
{ 
    int id; 
    int cnr; 
    int found = 0; 
    cin.clear(); 
    cin.sync(); 
    cout << "Enter lend id: "; 
    cin >> id; 
    cout<<id<<endl; 
    cout << "Enter customernumber: "; 
    cin >> cnr; 
    cout<<cnr<<endl; 
    for(int i = 0; i< antal; i++) 
    { 
     bib[i]->Borrow(cnr, id,found); 
    } 
    if(found == 0) 
     cout<<"ID does not exist!"<<endl; 
} 
void Bibliotek::givebackinit(vector <Lendobj*> & bib, int &antal) 
{ 
    int id; 
    int found = 0; 
    cout << "Enter lend id: "; 
    cin >> id; 
    for(int i = 0; i< antal; i++) 
    { 
     bib[i]->giveback(id,found); 
    } 
    if(found == 0) 
     cout<<"ID does not exist!"<<endl; 
} 
void Bibliotek::execfunc(vector <Lendobj*>& bib,char &c, int &antal) 
{ 

    switch(c) 
    { 
    case 'C': insert(bib,c,antal); break; 
    case 'F': insert(bib,c,antal); break; 
    case 'N': insert(bib,c,antal); break; 
    case 'J': insert(bib,c,antal); break; 
    case 'X': removeinit(bib,antal); break; 
    case 'H': help(); break; 
    case 'S': searchinit(bib,antal); break; 
    case 'B': borrowinit(bib,antal); break; 
    case 'R': givebackinit(bib,antal); break; 

    default: 
     cout << "Not a valid character" << endl; 
    } 
} 
+0

可能重複[什麼是未定義的引用/無法解析的外部符號錯誤,以及如何解決它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-外部符號錯誤和怎麼辦我修復) – sashoalm

回答

3

的問題是,你main FO rward-聲明這兩個函數

char getinput(); 
void execfunc(vector <Lendobj*> &bib,char &c, int &antal); 

沒有提供任何實現它們。然而,它看起來像你試圖引用Bibliotek類的現有功能:

char Bibliotek::getinput(); 
void Bibliotek::insert(vector <Lendobj*> &bib,char &type, int &antal); 

如果你想調用的Bibliotek成員函數,你需要使用成員語法如果函數是非靜態的給他們打電話(即需要引用非靜態成員變量),如果相應的函數是靜態的,則使用Bibliotek::getinput()。在任何一種情況下,您都需要刪除在main中插入的函數的前向聲明,並將其替換爲包含聲明Bibliotek類的頭。

+0

有實現我bibliotek.cpp文件。像這樣: – user2224247

+0

爲什麼它不適用於前向聲明?我是新來的語言。 – user2224247

+0

@ user2224247轉發聲明是提供定義的承諾。您不提供這些函數的定義,這就是連接器抱怨的原因。您提供了成員函數的定義,而不是普通的函數。你需要用'Bibliotek ::'前綴來調用它們,並將它們聲明爲'static'。 – dasblinkenlight

0

你的功能在類Bibliotek定義。因此,你必須給他們打電話合格:

c = Bibilotek::getinput(); 

Bibliotek::execfunc(biblio, c, antal); 

而且從main.cpp刪除全局性功能的多餘的聲明,因爲你可能不希望他們。

+0

你解決了它! Thak你。但我仍然有一個錯誤消息。它可以與包括文件:1> ------重建全部開始:項目:ou4sec,配置:調試的Win32 ------ 1> ou4sec.cpp 1> bibliotek.cpp 1>生成代碼... 1> MSVCRTD.lib(crtexew.obj):error LNK2019:無法解析的外部符號_WinMain @ 16在函數中引用___tmainCRTStartup 1> c:\ users \ jonas \ documents \ visual studio 2010 \ Projects \ ou4sec \ Debug \ ou4sec.exe:致命錯誤LNK1120:1周無法解析的外部 ==========全部重新生成:0成功,1失敗,0已跳過========== – user2224247

+0

我嘗試另一個版本的我項目,現在編譯好了。非常感謝你! – user2224247

相關問題