Search code examples
c++qtnon-member-functions

Name, Papers , Books was not declared in this scope non-member function C++


I have an error when I declare the non-member function 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;
    }
  }

compiler says name papers and books was not declared in this scope. I have tried several thing like making the function friend but then in the main it says that class Biblioagraphy has no member called listOverview();

This is my header:

#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

Solution

  • When you are going to define the function that are part of your class. You need to do it explicitly.

    instead of

    void listOverview()
    

    it should be

    void Bibliography::listOverview()