尝试处理类的向量成员时程序崩溃
Program crash when trying to work on the vector member of a class
你好,我尝试了我能想到的一切来修复崩溃......但没有得到任何东西。所以我会尝试粘贴程序的主要组件,这样也许有人可以帮助解决这个问题......程序编译没有错误,但它崩溃了。如果我运行程序,则首先选择"1",然后输入输入...当它到达save
部分时,它就会崩溃。代码的某些部分被注释掉了 ebcuase 我试图找到崩溃的原因。如果我忘记了什么,或者您需要一些其他细节,请询问对不起,代码太长了。主要功能和应用类别:
#include "Catalog.h"
#include "UIconsole.h"
#include "StudentRepository.h"
#include "StudentValidator.h"
using namespace std;
class Application{
private:
StudentRepository *stre;
StudentValidator *stvl;
Catalog *cat;
UIconsole *ui;
public:
Application();
~Application();
void run();
};
int main(){
Application app;
app.run();
return 0;
}
Application::Application(){
StudentRepository stre();
StudentValidator stvl();
Catalog cat();
UIconsole ui();
}
Application::~Application(){
}
void Application::run(){
ui->runUI();
}
UIconsole.h (如果输入是 choisen):
class UIconsole{
public:
UIconsole(Catalog *catalog):catalog(catalog){};
void runUI();
private:
Catalog *catalog;
void showMenu();
int getOption();
void addStudent();
用户界面控制台.cpp
int inp;
do{
showMenu();
inp = getOption();
switch(inp){
case 0: break;
case 1: addStudent();break;
}while(inp != 0);
}
void UIconsole::addStudent(){
string name;
int id,gr;
cout<<"Name: ";
cin>>name;
cout<<"ID: ";
cin>>id; cout<<endl;
cout<<"Group: ";
cin>>gr; cout<<endl;
catalog->addNewStudent(name,id,gr);
// try {
// catalog->addNewStudent(n,id,gr);
// cout<<"Student has been added!"<<endl;
// } catch (Exception &ex) {
// cout<<"Student HASN'T been added"<<ex.getMsg()<<endl;
// }
}
目录.cpp:
void Catalog::addNewStudent(string name, int id, int gr){
Student st(name, id,gr);
//StudentValidator.
studRepo.save(st);
}
学生回购.cpp:
void StudentRepository::save(Student A){
string B;
int check;
B = A.getName();
// check = findByName(B);
//
// if(check != 0){
students.push_back(Student(A));
// }
// else{
// throw RepoException("Name already exist in the database!");
// }
}
学生回购
class StudentRepository{
private:
vector <Student> students;
public:
vector <Student> getAll();
void save(Student);
void edit(Student);
void delByName(string);
void searchById();
int findByName(string name);
~StudentRepository();
};
跟踪:
Dr. Memory version 1.4.6 build 2 built on Mar 7 2012 10:14:04
Application cmdline: ""D:c++BeginL_6-8DebugL_6-8.exe""
Recorded 62 suppression(s) from default C:Program Files (x86)Dr. Memory/bin/suppress-default.txt
Error #1: UNINITIALIZED READ: reading register eax
# 0 _fu36___ZSt4cout [D:c++BeginL_6-8Debug/../UIconsole.cpp:90]
# 1 UIconsole::runUI() [D:c++BeginL_6-8Debug/../UIconsole.cpp:19]
# 2 Application::run() [D:c++BeginL_6-8Debug/../App.cpp:43]
# 3 main [D:c++BeginL_6-8Debug/../App.cpp:27]
Note: @0:00:10.842 in thread 5436
Note: instruction: mov (%eax) -> %eax
Error #2: UNADDRESSABLE ACCESS: reading 0x00000004-0x00000008 4 byte(s)
# 0 std::vector<>::push_back() [c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_vector.h:828]
# 1 StudentRepository::save() [D:c++BeginL_6-8Debug/../StudentRepository.cpp:27]
# 2 Catalog::addNewStudent() [D:c++BeginL_6-8Debug/../Catalog.cpp:7]
# 3 _fu36___ZSt4cout [D:c++BeginL_6-8Debug/../UIconsole.cpp:90]
# 4 UIconsole::runUI() [D:c++BeginL_6-8Debug/../UIconsole.cpp:19]
# 5 Application::run() [D:c++BeginL_6-8Debug/../App.cpp:43]
# 6 main [D:c++BeginL_6-8Debug/../App.cpp:27]
Note: @0:00:10.853 in thread 5436
Note: instruction: mov 0x04(%eax) -> %edx
Error #3: LEAK 21 direct bytes 0x006ff488-0x006ff49d + 0 indirect bytes
# 0 libstdc++-6.dll!Znwj
# 1 libstdc++-6.dll!ZNSs7reserveEj
# 2 libstdc++-6.dll!ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RSbIS4_S5_T1_E
# 3 _fu2___ZSt3cin [D:c++BeginL_6-8Debug/../UIconsole.cpp:81]
# 4 UIconsole::runUI() [D:c++BeginL_6-8Debug/../UIconsole.cpp:19]
# 5 Application::run() [D:c++BeginL_6-8Debug/../App.cpp:43]
# 6 main [D:c++BeginL_6-8Debug/../App.cpp:27]
DUPLICATE ERROR COUNTS:
SUPPRESSIONS USED:
ERRORS FOUND:
1 unique, 1 total unaddressable access(es)
1 unique, 1 total uninitialized access(es)
0 unique, 0 total invalid heap argument(s)
0 unique, 0 total warning(s)
1 unique, 1 total, 21 byte(s) of leak(s)
0 unique, 0 total, 0 byte(s) of possible leak(s)
ERRORS IGNORED:
84 still-reachable allocation(s)
(re-run with "-show_reachable" for details)
Details: C:UsersWarzaruAppDataRoaming/Dr. Memory/DrMemory-L_6-8.exe.8092.000/results.txt
要注意的第一个问题是应用程序构造函数。 应用程序类使用指针作为其成员,但您不会为这些指针分配数据。 您希望将应用程序构造函数的主体替换为:
[成员] = 新的 [member_type];
这也可能会解决您的崩溃问题。
你混淆了内存模型。
您的原始版本有两个问题
- 指针值未定义。不像在 java 中那样为空。调试器可能显示 null,但未定义指针的发布版本
- 构造函数将对象创建为自动变量,并在离开时销毁它们。那肯定不是你想要的。
''
Application::Application(){
StudentRepository stre();
StudentValidator stvl();
Catalog cat();
UIconsole ui();
}
如果要使用指针,请使用此方法
class Application{
private:
StudentRepository *stre;
StudentValidator *stvl;
Catalog *cat;
UIconsole *ui;
public:
Application();
~Application();
void run();
};
Application::Application(){
stre = new StudentRepository();
stvl = new StudentValidator ();
cat = new Catalog();
ui = new UIconsole ();
}
Application::~Application(){
delete stre;
delete stvl;
delete cat;
delete ui;
}
或者只是将对象添加为成员。 内部访问从"->"切换到"."
class Application{
private:
StudentRepository stre;
StudentValidator stvl;
Catalog cat;
UIconsole ui;
public:
// Application(); // no more necessary for this limited case
// ~Application(); // no more necessary for this limited case
void run();
};
相关文章:
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- C++多线程程序:变量定义为类成员的隔离错误
- 将 C# 对象(包含静态对象成员)作为参数传递给 C++/CLI 程序
- 当从成员类调用封装的std::begin时,程序崩溃
- 用于向公众公开数据成员的帮助程序结构
- 访问类成员的成员变量时 cpp 程序挂起
- 类成员作为 C++ 中 C 样式事件的事件处理程序
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 调用 lambda 的结构/类成员时程序崩溃并"bad_function_call"
- 如何在 Arduino 中使用 C++ 成员函数作为中断处理程序?
- 为什么调用单例类 Qt 消息处理程序成员函数会出现错误:缺少参数列表
- 包含格式错误的模板成员函数的格式正确的程序?
- ASYNC_WAIT处理程序班级成员具有取消处理
- 如何使用 lamba 作为连接处理程序调用 boost async_connect 作为成员函数
- CPPrestsdk 处理程序类在使用 STL 容器作为成员时崩溃
- 初始化静态成员类(用于信号处理程序)
- 如何从鼠标处理程序中调用成员功能
- 静态类成员在动态库中与主应用程序不同
- 尽管使用不存在的成员,但程序编译