English 中文(简体)
为什么会造成错误。
原标题:Why does this give a bad_alloc error?

目前,Im试图为学生设立成员职能,该成员的职能是用药进行扼杀,作为这一职能的理由,然后用数据制造学生的物体。 然而,这是否给我留下了错误。 我知道这一职能正在缩小,但在新目标产生之后,它就留下了这一错误。

错误:

./a.out

Please insert name for student:
Bob_Russel
terminate called after throwing an instance of  std::bad_alloc 
  what():  std::bad_alloc
Aborted

Constructor:

Student::Student(string tname){

  name = tname;

}

职能:

Student Student::readStudent(istream &i){

  Student *stud;

  string y;

  i >> y;

  stud = new Student(y);

  return  *stud;

}

测试

 #include "Student.h"

int main(){

  Student *stud3;
  cout << "
Please insert name for student:
";

  stud3->readStudent(cin);


return 0;

}
最佳回答

不仅有密码泄漏记忆(在<条码>上添加新的<条码>。 在<代码>main中,你正在使用一个未启用的电码readStudent。 这显然破坏了你的要求,使新陈列:bad_alloc。

进一步研究C++记忆管理和物体寿命。 这里确实没有必要使用点人。 页: 1 可修改如下:

int main() {
    Student stud3;
    std::cout << "Please insert name for student:" << std::endl;
    stud3.readStudent(std::cin);
}

如果你在<条码>内读到“条码>中<>条码/代码>上,这或许也会更好。 (作为<代码>std:string,然后将姓名直接传送到Student的构造者:

int main() {
    std::cout << "Please insert name for student:" << std::endl;
    // Read in the name.
    std::string name;
    std::cin >> name;
    // Create the student with the input name.
    Student stud3(name);
 }
问题回答

它希望你努力实施工厂方法。 如果是这样的话,那么你就重新遗漏了静态关键词和读写词的正确分子。

class Student{
public:
    Student(std::string tname);
    static Student* readStudent(std::istream &i);
private:
    std::string name
};

Student::Student(std::string tname) {
    name = tname;
}

Student* Student::readStudent(std::istream &i){
    std::string y;
    i >> y;
    return new Student(y);
}

int main(int argc, char* argv[]){
    Student *stud3 = NULL;

    std::cout << "
Please insert name for student:
";

    stud3 = Student::readStudent(cin);

    return 0;
}

你们正在利用新的、从未放开的跳板分配,因此,你们从记忆中走过来,拿着坏的_子。 每一条<代码>新 应为<代码>delete。

这样做不会错失:

Student Student::readStudent(std::istream& i)
{        
   std::string y;    
   i >> y;    
   return Student(y);    
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签