创建具有 new in 函数和"this is nullptr"异常的对象
Creating objects with new in function and "this is nullptr" exception
我一直在尝试创建一个模板类(称为List(,用于存储不同类型的对象。我创建了基类,就像我的程序和Human类中的基类一样。Base可以创建新的Human,并可以访问它们,它有一个指向List*first_h的(私有(指针(在每个列表中存储Human*me、List*next和List*first_h(列表中的first_h((。问题是,当我向我的基地添加超过1个人类时,我无法正确显示它们。我认为这是因为在Base方法中创建了新的Human(void Base::create_Human(字符串名称((,但我所做的一切都没有成功。这里有我的课程:
class Human
{
private:
string name;
public:
Human(string name) { this->name = name; }
void display() { cout << "My name: " << name << endl; }
};
template <class T>
class List
{
private:
T* me;
List <T>* next;
List <T>* first;
public:
void set_me(T* me) { this->me = me; }
T* get_me() { return this->me; }
void set_next(List* next) { this->next = next; }
List <T>* get_next() { return this->next; }
void set_first(List* first) { this->first = first; }
List <T>* get_first() { return this->first; }
void add(T*& created);
void display();
};
class Base
{
private:
List <Human>* first_h;
public:
void set_first_h(List <Human>*& first) { this->first_h = first; }
List <Human>* get_first_h() { return this->first_h; }
void create_human(string name)
{
Human* created = new Human(name);
this->first_h->add(created);
}
};
和方法:
template <class T>
void List<T>::add(T*& created)
{
List <T>* temp = this->get_first();
List <T>* new_list;
if ((this->get_me()) == nullptr)
{
this->set_next(nullptr);
this->set_me(created);
this->set_first(this);
}
else
{
new_list = new List <T>;
temp = this->get_first();
while (temp != nullptr)
{
temp = temp->get_next();
}
new_list->set_next(nullptr);
new_list->set_first(this->get_first());
temp->set_next(new_list);
}
}
template <class T>
void List<T>::display()
{
List <T>* temp_list = this;
T* temp;
if (temp_list == nullptr)
{
std::cout << "There is nothing!" << endl;
}
while (temp_list != nullptr)
{
temp = temp_list->get_me();
temp->display();
temp_list = temp_list->get_next();
}
std::cout << "End!" << endl;
}
以及我的主要功能:
int main()
{
Base Main;
List <Human>* first_h = new List <Human>();
Main.set_first_h(first_h);
Main.create_human("Jane");
Main.create_human("John");
Main.create_human("Mary");
Main.get_first_h()->display();
system("pause");
return 0;
}
对不起我的英语,提前谢谢你!
编辑:我发现了问题所在:附加功能:
new_list->set_next(nullptr);
new_list->set_me(created);
new_list->set_first(this->get_first());
temp->set_next(new_list);
我忘了:
new_list->set_me(created);
- 添加函数中的错误,正如您所写的那样
您的循环
while (temp != nullptr) { temp = temp->get_next(); }
运行直到温度为nullptr
,然后执行
temp->set_next(new_list);
因此,如您所见,在set_next()
内部,this
指针是nullptr
。
请学习如何使用调试器并查看调用堆栈。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Windows 10 环境中的"This version of XAudio2 is available only in Windows 8"错误
- "Attribute is protected within this context"继承和 .h 和.cpp文件
- CMake 错误:'target is not built by this project'
- 项目生成失败:"gcroot.h This document is opened by another project"
- 当从此到子类中的新对象时,将显示警告"destination for this 'memcpy' call is a pointer to dynamic class..."
- "is private within this context"被抛出用于不应是私有的函数(GCC 5.3.0,C++11)
- 如何抑制此内容 - qglobal.h:320:6: 警告: #warning "This version of Mac OS X is unsupported" ?
- GCC - 删除"is used uninitialized in this function"警告
- VC++ Winforms自动生成的项目无法调试VS2010:"This Project is out of Date"
- C++ DLL 到 C# 错误:"Attempted to read or write protected memory. This is often an indication that other
- G++ 编译错误"... is protected from within this context"而 clang 没有错误
- C++。与代码相关::块 IDE。"object is not declared in this scope"错误
- VS2015的多处理器编译在运行cl时会产生"not enough quota is available to process this command".exe