Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在类内使用成员函数_C++_Oop_Class_Object - Fatal编程技术网

C++ 无法在类内使用成员函数

C++ 无法在类内使用成员函数,c++,oop,class,object,C++,Oop,Class,Object,我目前正在学习关于类的知识,我的类实现文件中有一个问题 在我的类头/规范文件Book.h中,我有公共成员函数setPages 和作者 #ifndef AUTHOR_H #define AUTHOR_H class Author { private: std::string _name; int _numberOfBooks; public: Author(std::string& name) {_name = name;} void setNu

我目前正在学习关于类的知识,我的类实现文件中有一个问题

在我的类头/规范文件Book.h中,我有公共成员函数setPages

和作者

#ifndef AUTHOR_H
#define AUTHOR_H

class Author
{
private:
    std::string _name;
    int _numberOfBooks;
public:
    Author(std::string& name)
    {_name = name;}

    void setNumOfBooks (int numberOfBooks);
    int getNoOfBooks () const;

    bool operator==(std::string _name)
    {
        if (this->_name == _name)
            return true;
        else
            return false;
    }
};
#endif

直到@ahenderson决定将他的评论转化为答案:

Publisher.h中的bool operator==std::string name在您的示例中缺少大括号。这是代码中的错误还是复制粘贴错误


哎呀,这里没有支架

建议:简化运算符==方法:

表达式_name==name将返回true或false。无需将其放入返回true或false的if子句中

试试这个:

bool operator==(const std::string& name)
{
    return (_name == name);
}
在上面的示例中,直接计算表达式并返回结果

此外,如果变量以下划线“.”开头,则可能会遇到编译器问题。改变你的命名习惯,这样这个问题就不会引起人们的注意。两种常见的做法是附加后缀、名称或前缀,如m_name

Author.h和Publisher.h包含哪些文件?如果其中一个包含book.h,那么您在includes中有一个循环依赖项。另外:book const std::string&title,const Author&Author,const Publisher&Publisher:_titletite,_authorauther,_publisherpublisher{}尝试用类Author替换include Author.h和include Publisher.h;和类出版商;在Booh.h文件中。或者在C++11中,Bookstd::string title、Author Author、Publisher Publisher:_titlesd::movetitle、_authorstd::moveauthor、_publisherstd::movepublisher{}。请注意,每个参数都是按值传递的!!Publisher.h中的bool operator==std::string name在您的示例中缺少大括号。这是代码中的错误还是复制粘贴错误?
#ifndef PUBLISHER_H
#define PUBLISHER_H


class Publisher
{
private:
    std::string _name;
    std::string _address;
    std::string _phoneNumber;
public:
    Publisher (std::string& name)
    {_name=name;}

   void setAddress (std::string address);
   void setNumber (std::string phoneNumber);

    std::string getAddress () const;
    std::string getNumber () const;

    bool operator==(std::string name)
    {
        if (_name == name)
            return true;
        else 
            return false;
};

#endif
#ifndef AUTHOR_H
#define AUTHOR_H

class Author
{
private:
    std::string _name;
    int _numberOfBooks;
public:
    Author(std::string& name)
    {_name = name;}

    void setNumOfBooks (int numberOfBooks);
    int getNoOfBooks () const;

    bool operator==(std::string _name)
    {
        if (this->_name == _name)
            return true;
        else
            return false;
    }
};
#endif
bool operator==(std::string name)
{
    if (_name == name)
        return true;
    else 
        return false;
bool operator==(const std::string& name)
{
    return (_name == name);
}