C++超载<<操作员问题

发布于 2024-08-07 15:14:56 字数 960 浏览 7 评论 0 原文

我对 C++ 相当菜鸟,但对指针、取消引用等非常熟悉。我对 << 的重载有问题。类的运算符,因为它可以正常编译,但运行时会崩溃。感觉就像无限循环,但我不确定。这是代码,感谢任何帮助。

#include <string>
#include <iostream>

using namespace std;

class Person
{

private:

 string _name;
 Person* _manager;

public:

 Person(string name, Person *manager);
 Person(string name);

 friend ostream &operator<<(ostream &stream, Person &p);

};


Person::Person(string name, Person *manager)
{
 _name = name;
 _manager = manager;
}

Person::Person(string name)
{
 _name = name;
}

ostream &operator<<(ostream &stream, Person &p)
{
 Person* mgr = p._manager;

 stream << p._name << std::endl;
 stream << mgr->_name << std::endl;
 return stream;
}


int main()
{
 Person *pEmployee = new Person("John Doe Employee");
 Person *pManager = new Person("John Doe Manager", pEmployee);

 cout << *pEmployee;
 cout << *pManager;

 return 0;
}

I'm fairly noobish at C++, but very comfortable with pointers, dereferencing, etc. I'm having a problem with my overload of the << operator for a class, in that it compiles fine but crashes when run. It feels like an infinite loop, but I'm not certain. Here's the code, and any help is appreciated.

#include <string>
#include <iostream>

using namespace std;

class Person
{

private:

 string _name;
 Person* _manager;

public:

 Person(string name, Person *manager);
 Person(string name);

 friend ostream &operator<<(ostream &stream, Person &p);

};


Person::Person(string name, Person *manager)
{
 _name = name;
 _manager = manager;
}

Person::Person(string name)
{
 _name = name;
}

ostream &operator<<(ostream &stream, Person &p)
{
 Person* mgr = p._manager;

 stream << p._name << std::endl;
 stream << mgr->_name << std::endl;
 return stream;
}


int main()
{
 Person *pEmployee = new Person("John Doe Employee");
 Person *pManager = new Person("John Doe Manager", pEmployee);

 cout << *pEmployee;
 cout << *pManager;

 return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

关于从前 2024-08-14 15:14:56

在只有一个参数的构造函数中,您需要将 _manager 设置为 NULL/0。在您的操作员中对此进行测试<<如果 mgr->name 为 NULL/0,则不输出。就目前情况而言,您正在取消引用未初始化的指针。

Person::Person(string name)
{
    _name = name;
    _manager = 0;
}

ostream &operator<<(ostream &stream, Person &p)
{
    Person* mgr = p._manager;

    stream << p._name << std::endl;
    if (mgr)
        stream << mgr->_name << std::endl;
    return stream;
}

还有许多其他事情您可以做得更好,例如对参数使用 const 引用和使用构造函数初始值设定项列表,但它们不会成为问题的原因。您还应该解决传递给构造函数的管理器对象的所有权问题,以确保它不会被双重删除。

In your constructor with just a single argument, you need to set _manager to NULL/0. Test for this in your operator<< and don't output mgr->name if it is NULL/0. As it stands, you are dereferencing an uninitialised pointer.

Person::Person(string name)
{
    _name = name;
    _manager = 0;
}

ostream &operator<<(ostream &stream, Person &p)
{
    Person* mgr = p._manager;

    stream << p._name << std::endl;
    if (mgr)
        stream << mgr->_name << std::endl;
    return stream;
}

There are a number of other things you could do better, such as using const references on arguments and using the constructor initialiser list, but they wont be the cause of your problem. You should also address ownership issues with the manager object you pass in to the constructor to ensure it does not get double-deleted.

旧夏天 2024-08-14 15:14:56

您的 _manager 指针在构造第一个 Person 实例时未初始化,因此在您的运算符 << 中引用 p._manager崩溃。
除此之外,由于您调用 new 但不调用 delete,因此会出现内存泄漏。

your _manager pointer is not initialized upon constructing the first Person instance, hence referencing p._manager in your operator << crashes.
Besides that, you have a memory leak since you call new but not delete.

日暮斜阳 2024-08-14 15:14:56

您的 operator<< 实现不会检查 _manager 成员是否有效。如果没有管理器,您应该通过将指针设置为 0 来明确这一点。否则,指针值未定义,访问它会使程序崩溃。

(1) 如果构造函数中未提供任何内容,则您的 Person 类应将 _manager 设置为 0(空指针):

Person::Person(string name) : _name(name), _manager(0)
{
}

(2) 在您的 < code>operator<<,在取消引用之前检查指针:

if (mgr) {
    stream << mgr->_name << std::endl;
}

更好代码的一些提示:

(1) 将函数参数更改为接受 const string& 而不是 string< /代码>。这样,在调用函数/构造函数时不会复制字符串,而是作为常量引用传递。

(2) 让您的 operator<< 也接受 Personconst 引用会更干净,因为它不/应该不修改Person。这样,您也可以在有常量 Person 的地方使用该运算符。

Your implementation of operator<< does not check if the _manager member is valid. If there is no manager, you should make this explicit by setting the pointer to 0. Otherwise the pointer value is undefined and accessing it will crash your program.

(1) Your Person class should set _manager to 0 (null pointer) if none is supplied in the constructor:

Person::Person(string name) : _name(name), _manager(0)
{
}

(2) In your operator<<, check the pointer before dereferencing it:

if (mgr) {
    stream << mgr->_name << std::endl;
}

Some hints for better code:

(1) Change your function arguments to accept const string& instead of string. This way, the string is not copied when calling the function/constructor, but passed as a constant reference.

(2) It is cleaner to let your operator<< accept a const reference for the Person as well, since it does not/should not modify the Person. This way, you can use the operator also in places where you have a constant Person.

本王不退位尔等都是臣 2024-08-14 15:14:56
Person::Person(string name)
{
    _name = name;
}

你的经理是谁?

Person::Person(string name) : _manager(NULL)
{
    _name = name;
}

ostream &operator<<(ostream &stream, Person &p)
{
 Person* mgr = p._manager;

 stream << p._name << std::endl;
 if (mgr != NULL) { stream << mgr->_name << std::endl; }
 return stream;
}
Person::Person(string name)
{
    _name = name;
}

Who is your manager?

Person::Person(string name) : _manager(NULL)
{
    _name = name;
}

ostream &operator<<(ostream &stream, Person &p)
{
 Person* mgr = p._manager;

 stream << p._name << std::endl;
 if (mgr != NULL) { stream << mgr->_name << std::endl; }
 return stream;
}
黎夕旧梦 2024-08-14 15:14:56

您的 Person 实例 *pEmployee 没有设置 _manager。这可能是一个 NULL 指针。

Your Person instance *pEmployee does not have the _manager set. This may be a NULL pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文