返回语句返回一个空指针值而不是期望的值
Return Statement Returning a Null Pointer Value and Not the Desired Value
我通过调试运行了这个,在String Substring函数中,直到返回语句,一切都正常工作。
在下面的代码中,'returnString'在返回行处具有正确的值。但是,只要我转到下一行(后面的右括号),它就变成:
{Text=0x003ed0e0 "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ... }String
,我追溯到析构函数,是删除String时的值。
现在,我认为值只有在传递之后才会被删除,但看起来它是先被删除的。你们知道怎么解决吗?就像我上面说的,这个函数工作得很完美(至少看起来是这样),只是它返回值的方式有问题。
调用我的字符串函数的行:String LocalString((Address.Substring(0, atIndex)));
(地址在各自的头文件中被声明为'private'下的字符串)
Substring位于下标操作符的中间位置。如果我似乎遗漏了一个功能或文件,请要求它。谢谢你的阅读和(我希望)帮助。
String.h文件:#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] != ' ')
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]!=' ')
{
substring[x] = str[x + startPosition];
x++;
}
substring[x]=' ';
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;
};
String LocalString((Address.Substring(0, atIndex)));
String DomainString((Address.Substring(atIndex + 1)));
// or, in simpler syntax:
/*
* String hell0 = String andGoodbye;
* String smile = String andWave;
*/
尽管注释// Assignment operator to perform deep copy
,类没有用户定义的赋值操作符。
计算机生成的默认赋值操作符执行浅拷贝。一旦其中一份副本被销毁,所包含的文本将丢失。
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- 什么 /可以/ 你从一个函数返回. 并期望它在到达时还活着?
- GCC:我什么时候可以期望返回大型C++对象是有效的
- CUDA FFT不会返回我期望的值
- C++我可以期望所有编译器都不会销毁返回的副本
- 尝试打印出指针的值,期望指针指向的对象的地址,相反,编译器返回对象的值
- 二叉树搜索没有返回期望的值
- Std::string::find_first_of没有返回期望的值
- HMAC-SHA1不返回期望的哈希值
- Getenv函数没有返回期望的答案
- sizeof打包位域返回4,但我期望是1.它是怎么来的,我怎么能解决
- Get函数没有返回我所期望的
- 返回语句返回一个空指针值而不是期望的值
- 如何通过引用返回一个向量,隐式期望一个空向量作为输入