C++过载>>对于 mac 上的 ifstream 指针被释放未分配
我正在尝试以下代码,但失败并出现以下错误:
malloc: *** error for object 0x10000d8c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
以下是文件 input.txt 的内容:它具有完全权限,并且文件已在调试器中成功打开。请帮忙。
Jacob Anderson
Michael Thomson
Joshua Smith
Mathew Matheis
Ethan Evans
Emily Drake
Emma Patterson
Madison McPhee
Hannah Briens
Ashley Schmidt
。
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <list>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
struct DataType {
string lastname; // (Key) Student's Last Name
string firstname; // Student's First Name
string getKey () const
{ return lastname; } // Returns the key field
};
ostream& operator << (ostream& os, DataType myData) {
os<<myData.firstname<< " "<<myData.lastname;
return os;
}
bool operator < (DataType lhs, DataType rhs) {
if (lhs.firstname < rhs.firstname)
return true;
return false;
}
int main() {
ifstream studentFile ("input.txt"); // Student file
list <DataType> students; // Students
DataType currStudent; // One Student (has firstname,lastname)
if (! studentFile.is_open())
{
return -1;
}
while (studentFile >> currStudent.firstname >> currStudent.lastname) {
students.push_back(currStudent);
}
list<DataType>::iterator i = students.begin();
while (i != students.end()) {
cout << *i << endl ;
++i;
}
}
I am trying the following code and it fails with the following error:
malloc: *** error for object 0x10000d8c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
Here are contents of file input.txt : It has full permissions and file is successfully opened in debugger. Please help.
Jacob Anderson
Michael Thomson
Joshua Smith
Mathew Matheis
Ethan Evans
Emily Drake
Emma Patterson
Madison McPhee
Hannah Briens
Ashley Schmidt
.
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <list>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
struct DataType {
string lastname; // (Key) Student's Last Name
string firstname; // Student's First Name
string getKey () const
{ return lastname; } // Returns the key field
};
ostream& operator << (ostream& os, DataType myData) {
os<<myData.firstname<< " "<<myData.lastname;
return os;
}
bool operator < (DataType lhs, DataType rhs) {
if (lhs.firstname < rhs.firstname)
return true;
return false;
}
int main() {
ifstream studentFile ("input.txt"); // Student file
list <DataType> students; // Students
DataType currStudent; // One Student (has firstname,lastname)
if (! studentFile.is_open())
{
return -1;
}
while (studentFile >> currStudent.firstname >> currStudent.lastname) {
students.push_back(currStudent);
}
list<DataType>::iterator i = students.begin();
while (i != students.end()) {
cout << *i << endl ;
++i;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不出代码有任何明显的错误。正在进行一些不必要的复制(各种运算符应该采用
DataType &
(实际上,最好是const DataType &
)而不是像现在这样的对象,以防止对象我还会删除 stdio.h 的内容,因为您在此处显示的代码不需要它,但是是否还有其他代码。你不给我们看吗?
I can't see anything obviously wrong with the code. There's some unnecessary copying going on (the various operators should take
DataType &
(actually, preferablyconst DataType &
) rather than objects as they do now to prevent the objects from being copied. I'd also remove the inclusion of stdio.h as you don't need that for the code you're showing here.None of the above should trigger the error you're seeing, though. Is there any other code you're not showing us?
代码对我来说看起来没问题——我想说问题来自其他地方(可能是安装问题?)你确实有一些部分不太好,但没有什么应该导致重大问题(例如
DataType::getKey
从未使用过,operator<(DataType, DataType)
从未使用过,operator<<
应该采用 const 引用而不是价值)。The code looks all right to me -- I'd say the problem is from elsewhere (possibly an installation problem?) You do have some pieces that aren't exactly great, but nothing that should cause a major problem (e.g.
DataType::getKey
is never used,operator<(DataType, DataType)
is never used,operator<<
should probably take a const reference instead of a value).