返回语句返回空指针值,而不是期望值 [英] Return Statement Returning a Null Pointer Value and Not the Desired Value

查看:203
本文介绍了返回语句返回空指针值,而不是期望值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过调试运行,在String Substring函数中,一切正常,直到return语句。



'returnString'在下面的代码中,在返回行时具有正确的值。然而,当我去到下一行(后直接右括号),它改变为:



的{text = 0x003ed0e0îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ。 ..}字符串



我追溯到析构函数,是串中删​​除时,已值。



现在,我会想到它是只传递后的值会被删除,但现在看来似乎是越来越先将其删除。你们知道如何解决这一问题?就像我上面所说的,功能如何工作



调用我的字符串函数的行:字符串LocalString((Address.Substring(0,atIndex))); (地址声明在相应的头文件下的私人的字符串)



子字符串稍微过半,紧接着索引运算符。如果它似乎我缺少一个函数或文件,请求。感谢您阅读和(我希望)帮助。



String.h 文件:

  #pragma once 

#include< iostream>
#include< sstream>
using namespace std;

// C ++封装ASCII C字符串的String类别
class String
{
public:
//默认构造函数
String ()
{
Text = NULL;
}

//必须具备:拷贝构造函数执行深拷贝
字符串(常量字符串和放大器;源)
{
文本= NULL;
//调用赋值运算符以执行深层复制
* this = source;
}

//使用C字符串初始化此String的初始构造函数
String(const char * text)
{
Text = NULL ;
//调用赋值运算符以执行深层复制

* this = text;

}

//析构函数
〜String()
{
delete [] Text;
}

//返回C字符串文本中的字符数; NULL终止符不计数
static int GetLength(const char * text)
{
int x = 0;
while(text [x]!='\0')
x ++;

return x;
}

//执行深层复制的赋值运算符
String&运算符=(const char * text)
{
// D处置旧文本
delete [] Text;

// +1帐户NULL-terminator
int trueLength = GetLength(text)+ 1;

//动态分配堆上的字符
Text = new char [trueLength];

//将所有字符从源复制到文本; +1个帐户的NULL终止符
for(int i = 0; i Text [i] = text [i];

return * this;
}

//如果没有指定长度,那么从子指定startPosition跨越直到这个字符串
//年底抛出异常指定startPosition时是出界
String Substring(int startPosition,int length = 0)const
{
char * str = this-> GetText();
int strLength = length;
int x = 0

char * substring = new char [strLength];

而(X LT; strLength&放大器;&放大器;!STR [X +指定startPosition] ='\0')
{
子[X] = STR [X + startPosition];
x ++;
}
substring [x] ='\0';

String returnString = substring;


return returnString;
}

//返回字符串中的字符数; NULL终止符不计数
int GetLength()const
{
return GetLength(Text);
}
private:
//封装的C字符串
char * Text;
};

$ b


$ b

  String LocalString((Address.Substring(0,atIndex))); 
String DomainString((Address.Substring(atIndex + 1)));
//或,用更简单的语法:
/ *
* String hell0 = String andGoodbye;
* String smile = String andWave;
* /


解决方案

尽管注释 //执行深拷贝的分配操作该类没有用户定义的赋值运算符。



默认的计算机生成的赋值运算符执行浅拷贝。包含的文本将在一个副本销毁后丢失。


I ran this through debug, and in the String Substring function, everything works up until the return statement.

'returnString', in the code below, has the correct value when at the return line. However, as soon as I go to next line (the closing bracket directly after), it changes to:

{Text=0x003ed0e0 "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ... }String

which I traced back to the destructor, is the value the String has when deleted.

Now, I would have thought that the value would be deleted only after it's passed, but it seems like it's getting deleted first. You guys know how to fix this? Like I said above, the function works perfectly (at least, it looks like it), there's just something wrong with how it's returning the value.

The line that calls on my string function: String LocalString((Address.Substring(0, atIndex))); (Address is declared a String under 'private' in the respective header file)

Substring is a little past halfway down, right after the index operator. If it seems like I'm missing a function or a file, ask for it. Thanks for reading and (I'm hoping) the help.

The String.h file:

#pragma once

#include <iostream>
#include <sstream>
using namespace std;

// C++ String class that encapsulates an ASCII C-string
class String
{
public:
    // Default constructor
    String()
    {
        Text = NULL;
    }

    // MUST HAVE: Copy-constructor that performs deep copy
    String(const String& source)
    {
        Text = NULL;
        // Call the assignment operator to perform deep copy
        *this = source;     
    }

    // Init-constructor to initialize this String with a C-string
    String(const char* text)
    {
        Text = NULL;
        // Call the assignment operator to perform deep copy

        *this = text;

    }   

    // Destructor
    ~String()
    {
        delete[] Text;
    }

    // Returns the count of characters in a C-string text; NULL-terminator is not counted
    static int GetLength(const char* text)
    {
        int x = 0;
        while(text[x] != '\0')
            x++;

        return x;
    }

    // Assignment operator to perform deep copy
    String& operator = (const char* text)
    {
        // Ddispose of old Text
        delete[] Text;

        // +1 accounts for NULL-terminator
        int trueLength = GetLength(text) + 1;

        // Dynamically allocate characters on heap
        Text = new char[trueLength];

        // Copy all characters from source to Text; +1 accounts for NULL-terminator
        for ( int i = 0; i < trueLength; i++ )
            Text[i] = text[i];

        return *this;
    }

    // if length is not specified, then the substring spans from startPosition until the end of this String
    // throws an exception when startPosition is out of bounds
    String Substring(int startPosition, int length=0) const
    {
        char * str = this->GetText();
        int strLength = length;
        int x = 0;

        char* substring = new char[strLength];

        while(x < strLength && str[x+startPosition]!='\0')
        {
            substring[x] = str[x + startPosition];
            x++;
        }
        substring[x]='\0';

        String returnString = substring;


        return returnString;    
    }

    // Returns the count of characters in the String; NULL-terminator is not counted
    int GetLength() const
    {
        return GetLength(Text);
    }
private:
    // The encapsulated C-string
    char* Text;
};

somewhere in main.cpp...

String LocalString((Address.Substring(0, atIndex)));
String DomainString((Address.Substring(atIndex + 1)));
// or, in simpler syntax:
/*
 * String hell0 = String andGoodbye;
 * String smile = String andWave;
 */

解决方案

Despite the comment // Assignment operator to perform deep copy the class doesn't have a user defined assignment operator.

The default, computer generated, assignment operator performs a shallow copy. The contained text will be lost as soon as one copy is destroyed.

这篇关于返回语句返回空指针值,而不是期望值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆