-3

它与main中的两个函数有关。获取输入和执行函数。它们在 Bibliotek 类中定义。我搜索了这个。可以与前向声明有关吗?错误信息是:

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 &)" (?execfunc@@YAXAAV?$vector@PAVLendobj@@V?$allocator@PAVLendobj@@@std@@@std) referenced in function _main
1>ou4sec.obj : error LNK2019: unresolved external symbol "char __cdecl getinput(void)" (?getinput@@YADXZ) referenced in function _main
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 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;
    }
}
4

2 回答 2

3

问题是您的main前向声明这两个功能

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该类的标头。

于 2013-05-20T12:20:18.190 回答
0

您的函数在类中定义Bibliotek。因此,您必须称它们为合格的:

c = Bibilotek::getinput();

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

并从 中删除全局函数的多余声明main.cpp,因为您可能不想要它们。

于 2013-05-20T12:22:52.043 回答