简体   繁体   English

Class 在 C++ 中不包含名为...的成员

[英]Class contains no member named ... in C++

Assuming I am making a library of books in C++ this way:假设我正在以这种方式在 C++ 建立一个图书馆:

#include <iostream>
#include <string>
#include <vector>

class Book
{
public:
     Book(string name, string author)
};

Simple, just a constructor, now I create a vector of Book and push books back:很简单,只是一个构造函数,现在我创建一个Book向量并将书籍推回去:

int main()
{
   vector<Book> books;
   books.push_back(Book("Gatsby", "Fitzgerald"));

But when I try to print out some member (name or author):但是当我尝试打印出一些成员(姓名或作者)时:

   cout << books[0].name << endl;

   return 0;
}

My boy compiler is angry:我的男孩编译器很生气:

error: ‘__gnu_cxx::__alloc_traits >::value_type {aka class Book}’ has no member named ‘name’
     cout << books[0].name << endl;

I'm a relative beginner, does this approach make sense at all?我是一个相对初学者,这种方法是否有意义? And if it does, what did I do wrong?如果是这样,我做错了什么?

Thank you!谢谢!

The class Book has no members to store name and author. class Book没有存储姓名和作者的成员。 And, the constructor that you defined is syntactically wrong.而且,您定义的构造函数在语法上是错误的。

With public data members, it would look like this:对于公共数据成员,它看起来像这样:

class Book
{
public:
    // data members
    std::string name;
    std::string author;

    // parameterized constructor
    Book( std::string name, std::string author )
    {
        this->name = name;
        this->author = author;
    }
};

Please note that:请注意:

  1. exposing data members like that is in violation of data-hiding principle of OOP. Ideally, the data members should be private and adequate accessor methods should be used.像这样公开数据成员违反了 OOP 的数据隐藏原则。理想情况下,数据成员应该是private的,并且应该使用适当的访问器方法。
  2. the assignments of name and author in the body of the constructor is just for your understanding.构造函数主体中名称和作者的分配仅供您理解。 If you've already studied the initializer list for constructor then use that.如果您已经研究了构造函数的初始化列表,请使用它。

Here's an example ( live ):这是一个例子(现场):

#include <iostream>
#include <string>
#include <vector>

class Book final
{
public:
    // constructor with initializer list
    Book( std::string name_, std::string author_ ) : name{name_}, author{author_} {}

    // accessor methods
    std::string getName() const { return name; }
    std::string getAuthor() const { return author; }

private:
    std::string name;
    std::string author;
};

int main()
{
    std::vector<Book> books;

    books.push_back( Book{"The Alchemist", "Paulo Coehlo"} );
    books.push_back( Book{"Fight Club", "Chuck Palahniuk"} );
    books.push_back( Book{"No Country for Old Men", "Cormac McCarthy"} );

    books.emplace_back( "Brave New World", "Aldous Huxley" );
    books.emplace_back( "1984", "George Orwell" );
    books.emplace_back( "Animal Farm", "George Orwell" );

    for ( const auto& book : books )
    {
        std::cout << book.getName() << " by " << book.getAuthor() << '\n';
    }

    return 0;
}

Some relevant threads to read:一些相关的线程阅读:

Assuming I am making a library of books in C++ this way:假设我正在以这种方式用 C++ 创建一个图书馆:

#include <iostream>
#include <string>
#include <vector>

class Book
{
public:
     Book(string name, string author)
};

Simple, just a constructor, now I create a vector of Book and push books back:很简单,只是一个构造函数,现在我创建一个Book向量并将书籍推回:

int main()
{
   vector<Book> books;
   books.push_back(Book("Gatsby", "Fitzgerald"));

But when I try to print out some member (name or author):但是当我尝试打印某个成员(姓名或作者)时:

   cout << books[0].name << endl;

   return 0;
}

My boy compiler is angry:我的男孩编译器很生气:

error: ‘__gnu_cxx::__alloc_traits >::value_type {aka class Book}’ has no member named ‘name’
     cout << books[0].name << endl;

I'm a relative beginner, does this approach make sense at all?我是一个相对初学者,这种方法是否有意义? And if it does, what did I do wrong?如果是这样,我做错了什么?

Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM