繁体   English   中英

受保护成员的C ++运行时错误

[英]C++ run time error with protected members

我正在尝试做一个作业分配,其中我们使用链接堆栈在指定点将字符串插入字符串,因此使用struct和typedef。 无论如何,当我尝试在InsertAfter方法内的StringModifier类中访问stringLength时,出现运行时错误,并且我无法弄清楚问题出在哪里。 我应该能够访问和修改变量,因为它受到保护并且派生类是公共继承的。

struct StringRec
{
    char theCh;
    StringRec* nextCh;
};

typedef StringRec* StringPointer;

class String
{
    public:
        String();
        ~String();
        void SetString();
        void OutputString();
        int GetLength() const;
    protected:
        StringPointer head;
        int stringLength;
};

class StringModifier : public String
{
    public:
        StringModifier();
        ~StringModifier();
        void InsertAfter( StringModifier& subString, int insertAt );
};

void StringModifier::InsertAfter( StringModifier& subString, int insertAt )
{
// RUN TIME ERROR HERE
    stringLength += subString.stringLength;
}

在MAIN

StringModifier test;
StringModifier test2;

cout << "First string" << endl;
test.SetString();
test.OutputString();
cout << endl << test.GetLength();
cout << endl << "Second string" << endl;
test2.SetString();
test2.OutputString();
cout << endl << test2.GetLength();
cout << endl << "Add Second to First" << endl;
test.InsertAfter( test2, 2 );
test.OutputString();
cout << endl << test.GetLength();

//String Class

String::String()
{
    head = NULL;
    stringLength = 0;
}

String::~String()
{
// Add this later
}

void String::SetString()
{
    StringPointer p;
    char tempCh;

    int i = 0;
    cout << "Enter a string: ";
    cin.get( tempCh );
// Gets input and sets it to a stack
    while( tempCh != '\n' )
    {
        i++;
        p = new StringRec;
        p->theCh = tempCh;
        p->nextCh = head;
        head = p;
        cin.get( tempCh );
    }

    stringLength = i;
}

void String::OutputString()
{
    int i = stringLength;
    int chCounter;
    StringPointer temp;
// Outputs the string bottom to top, instead of top to bottom so it makes sense when read
    while( head != NULL && i > 0 )
    {
        temp = head;
        chCounter = 0;
        while( temp != NULL && chCounter < (i-1) )
        {
            temp = temp->nextCh;
            chCounter++;
        }
        cout << temp->theCh;
        i--;
    }
}

int String::GetLength() const
{
    return stringLength;
}

StringModifier类具有空的构造函数和析构函数。

只是一个提示:C ++中的运行时错误与公共/受保护/私有访问完全无关。 编译器在编译代码时已检查是否遵守所有类成员访问规则。

运行时错误意味着您的程序中存在错误,很可能是某种形式的内存损坏。

您确定您的运行时错误实际上发生在InsertAfter函数中吗? 在我看来,当您修改stringLength时,您应该在OutputString中遇到访问冲突,因为您实际上尚未添加字符。 在您的while循环中添加(temp!= NULL)子句几乎可以避免这种情况-但请查看如果由于temp变为NULL而实际退出循环会发生什么...

回应您的评论:恐怕我仍然对该运行时错误发生的位置有些怀疑! 如果代码确实是问题中给出的,并且假设您没有搞砸的构建,那么在InsertAfter中拥有AV或其他内容几乎是不可能的(好吧,这在C ++中是很危险的事情,但是嘿-您只是在更改在堆栈上分配的对象的成员的值)。 请注意,您不能仅仅因为不调用而导致错误消失,就无法判断出InsertAfter方法中发生了错误-实际上,OutputString中的错误仅通过对InsertAfter的调用而暴露,因此,如果InsertAfter方法出现,则该错误消失通话被删除。 要进行检查,请在可疑语句之前和之后使用调试器,或在InsertAfter中添加一些日志记录。

您确定运行时错误的位置吗? 您是如何检查的? 我看到一个非常可疑的地方:

while( temp != NULL && chCounter < (i-1) )
{
        temp = temp->nextCh;
        chCounter++;
}
cout << temp->theCh; // temp can be NULL here?

temp!= NULL在循环标头中始终为true,或者在某些情况下确实存在NULL指针取消引用,这本身就是运行时错误。

您可以尝试在Valgrind(免费)或Purify(可能不是免费)下运行程序,以尽早发现内存错误。 错误消息也应该更加清晰。

另外,只需在调试器下运行该程序,然后使其崩溃即可检查其状态。 是您所期望的吗?

感谢大家的帮助。 事实证明,我一开始就将事情添加到列表中是错误的。 我没有将其放入堆栈,而是将其放入队列,并且一切正常! 我接受了你们的建议,并在其他地方寻找了问题所在。 谢谢!

void String::SetString()
{
    StringPointer p, last;
    char tempCh;
    last = head;
    int i = 0;
    cout << "Enter a string: ";
    cin.get( tempCh );

    while( tempCh != '\n' )
    {
        i++;
        if( i == 1 )
        {
            p = new StringRec;
            p->theCh = tempCh;
            head = p;
            last = p;
        }
        else
        {
            p = new StringRec;
            p->theCh = tempCh;
            p->nextCh = last;
            last->nextCh = p;
            last = p;
        }
        cin.get( tempCh );
    }

    last->nextCh = NULL;

    stringLength = i;
}

暂无
暂无

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

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