0

声明非成员函数 listOverview(); 时出错

  void listOverview()
  {

  std::cout << "Overview of books of " << name << std::endl;
  for (auto p : books)
    {
    std::cout << p.toString() << std::endl;
    }
  std::cout << "Overview of papers of " << name << std::endl;
  for (auto p : papers)
    {
    std::cout << p.toString() << std::endl;
    }
  }

编译器说名称文件和书籍未在此范围内声明。我已经尝试了几件事,比如让函数成为朋友,但主要是它说类 Bibliography 没有名为 listOverview() 的成员;

这是我的标题:

#ifndef BIBLIOGRAPHY_H
#define BIBLIOGRAPHY_H
#include <string>
#include <vector>


class Book;
class Paper;

class Bibliography
  {
  public:
    Bibliography(const std::string & aName);

    void addBook(const Book * newBook);
    void addPaper(const Paper *newPaper);
    int giveNrOfBooks() const {return books.size();}
    int giveNrOfPapers() const {return papers.size();}
    void listOverview();
//    std::vector<std::shared_ptr<Publication>> givePubWithIFHigherThan(float value) const;
//    void replaceIFofJournalsMatching(const std::regex journal, float newIF);
  private:
    const std::string name;
    std::vector<const Book *> books;
    std::vector<const Paper *> papers;

  };

#endif // BIBLIOGRAPHY_H
4

2 回答 2

1

如果listofOverview是一个自由函数,则没有books在内部声明或通过参数传递(不,你不想使用全局变量)。

如果你认为它是一个类成员,你应该写void Bibliography::listOfOverview()但它是一个类成员而不是一个自由函数。

于 2016-01-07T10:57:02.807 回答
1

当您要定义属于您的类的函数时。你需要明确地做到这一点。

代替

void listOverview()

它应该是

void Bibliography::listOverview()
于 2016-01-07T10:55:44.540 回答