1

まず最初に、この数時間に受けたすべての支援に感謝します。私はこの問題に苦労してきました。生のポインタから一意のポインタに変換する方法で、多くのエラーが発生しました。しかし、このコミュニティの助けを借りて、私のプログラムが最終的にエラーなしでコンパイルされたことに感謝しています. しかし、私はまだそこにいないと思います。ゴールまであと1分という感じなので、解けるまで諦めません。プログラムが実行されるとすぐにクラッシュし、スタック オーバーフローが発生して例外がスローされます。コンストラクターのクラスメンバーとして一意のポインターを宣言および初期化する方法がまったく正しくないため、コンストラクターを呼び出した瞬間からクラッシュする必要があると思います。このエラーを修正するために何をすべきか教えてください。ありがとう。

これは私のメインの cpp ファイルです:

#include"ContactList.h"
#include<memory>

using namespace std;

int main()
{
    //ContactList* cl1 = new ContactList();

    unique_ptr<ContactList> cl1(new ContactList());
    string name;

    while(true)
    {
        cout << "Enter a name or q to quit: " << endl;
        cin >> name;
        if(name == "q")
            break;
        cl1->addToHead(name);
    }

    cl1->PrintList();
    return 0;
}

ContactList.h

#pragma once
#include"Contact.h"
#include<memory>

using namespace std;

class ContactList
{
public:
    ContactList();
    void addToHead(const std::string&);
    void PrintList();

private:
    //Contact* head;
    unique_ptr<Contact> head;
    int size;
};

ContactList.cpp

#include"ContactList.h"
#include<memory>

using namespace std;

ContactList::ContactList(): head(new Contact()), size(0)
{
}

void ContactList::addToHead(const string& name)
{
    //Contact* newOne = new Contact(name);
    unique_ptr<Contact> newOne(new Contact(name));

    if(head == 0)
    {
        head.swap(newOne);
        //head = move(newOne);
    }
    else
    {
        newOne->next.swap(head);
        head.swap(newOne);
        //newOne->next = move(head);
        //head = move(newOne);
    }
    size++;
}

void ContactList::PrintList()
{
    //Contact* tp = head;
    unique_ptr<Contact> tp(new Contact());
    tp.swap(head);
    //tp = move(head);

    while(tp != 0)
    {
        cout << *tp << endl;
        tp.swap(tp->next);
        //tp = move(tp->next);
    }
}

Contact.h

#pragma once
#include<iostream>
#include<string>
#include<memory>

class Contact
{
    friend std::ostream& operator<<(std::ostream& os, const Contact& c);
    friend class ContactList;

public:
    Contact(std::string name = "none");

private:
    std::string name;
    //Contact* next;    
    std::unique_ptr<Contact> next;
};

Contact.cpp
#include"Contact.h"

using namespace std;

Contact::Contact(string n):name(n), next(new Contact())
{
}

ostream& operator<<(ostream& os, const Contact& c)
{
    return os << "Name: " << c.name;
}

これは私が得るエラーです:

Unhandled exception at 0x77E3DEFE (ntdll.dll) in Practice.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x002B2F58).
4

2 に答える 2

1

メソッドに問題があります。いくつかの項目を観察するだけで反復処理する場合ContactList::PrintList()は必要ありません。アイテムを観察する ときは、生のポインターで問題ありません。unique_ptr

一般に、生のポインターを所有する ことは良くありませんが (いくつかの特別な場合を除いて)、生のポインターを監視することは問題ありません。


さらに、デフォルト コンストラクターでは、空のwithを割り当ててデータ メンバーに割り当てるContactList必要がないことにも注意してください。デフォルト コンストラクターは自動的に に初期化されます。Contactnewhead unique_ptrunique_ptrheadnullptr

通常、あるコレクションのコンテンツを印刷してもコレクション内の項目は変更されないため、ContactList::PrintList()メソッドはconst適切なconst-correctnessとしてマークする必要があることにも注意してください。

最後に、関数内のContactList割り当てはスタック上main()で簡単に行うことができます:

ContactList cl;

この場合、使用する必要はありunique_ptrません (Java や C# スタイルではなく、C++ でプログラミングしてください)。

また、スタイルに関する注意: 大文字で始まるメソッド (例: PrintList()) と小文字で始まるメソッド (例: ) があるのは好きではありません: 1 つのスタイルをaddToHead()選択し、それに一貫性を持たせてください (ソース ファイルのリストで)。プロジェクト全体レベルではない場合)。


以下は、いくつかの修正が適用されたコードに基づいた単一のソース ファイル テスト コードです。

私はそれをコンパイルし、VC10 (Visual Studio 2010 SP1) で少しテストしました。それはうまくいくようです:

C:\Temp>test.exe
Enter a name or q to quit:
Bob
Enter a name or q to quit:
Jane
Enter a name or q to quit:
John
Enter a name or q to quit:
Mary
Enter a name or q to quit:
q
[Contact name: Mary]
[Contact name: John]
[Contact name: Jane]
[Contact name: Bob]

コンパイル可能なソース コードは次のとおりです。

#include <iostream>
#include <memory>
#include <ostream>
#include <string>
using namespace std;

// "Imaginary" Contact implementation (you didn't provide it)
struct Contact {
    Contact() {}
    explicit Contact(const string& n) : name(n) {}

    string name;    
    unique_ptr<Contact> next;
};

ostream& operator<<(ostream& os, const Contact& c) {
    os << "[Contact name: " << c.name << "]";
    return os;
}

class ContactList {
public:
    ContactList();
    void AddToHead(const string&);
    void PrintList() const;

private:
    unique_ptr<Contact> head;
    int size;
};

ContactList::ContactList()
  : size(0) {
  // No need to initialize head pointer.
  // It will be automatically initialized to nullptr.
}

void ContactList::AddToHead(const string& name) {
    unique_ptr<Contact> newOne(new Contact(name));
    if(head == 0) {
        head.swap(newOne);
    } else {
        newOne->next.swap(head);
        head.swap(newOne);
    }
    size++;
}

void ContactList::PrintList() const {
    const Contact * pContact = head.get();   
    while (pContact != nullptr) {
        cout << *pContact << endl;
        pContact = pContact->next.get();
    }
}

int main() {
    // No need to allocate ContactList using unique_ptr.
    // Stack scoped-based allocation is just fine.
    ContactList cl;    

    while (true) {
        cout << "Enter a name or q to quit: " << endl;
        string name;
        cin >> name;
        if (name == "q")
            break;
        cl.AddToHead(name);
    }

    cl.PrintList();
}
于 2013-10-07T10:25:27.473 に答える