分段错误(核心转储)C++
segmentation fault (core dump) c++
我一直在为我的 class 作业编写代码,当我使用 g++ 命令在终端中运行它时,它给我分段错误。代码很大,但第一部分,当我插入地图元素时,它可以工作(因为一旦它编写了命令然后被粉碎)然后当它必须转换时(这是使用重新定义运算符的赋值)它崩溃了。有人可以帮助我吗?
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <typeinfo>
#include "druga_verzija.h"
using namespace std;
//constructor
Posiljka::Posiljka(){
m.clear();
}
//how to print my map
ostream&operator<<(ostream&f, Posiljka&p){
map<string , int>::iterator it;
it=p.m.begin();
cout << it->second << "x" << it->first;
for( it=p.m.begin() ; it != p.m.end() ; ++it){
cout << " " << it->second << "x" << it->first;
}
}
//how to insert elements
Posiljka&Posiljka::operator<<(string s){
if(m.empty()){
m.insert(pair<string,int>(s,1));
return *this;
}
map<string ,int>::iterator it=m.begin();
for(; it != m.end() ; ++it){
if( it->first == s){
it->second++;
return *this;
}
if( it->first > s){
m.insert( it, pair<string,int>(s , 1));
return *this;
}
}
if(it == m.end()){
m.insert(pair<string,int>( s, 1));
return *this;
}
}
//how to delete them
Posiljka&Posiljka::operator>>(string s){
if(m[s]){
m[s]=m[s]-1;
if(!m[s]){
m.erase(s);
return *this;
}
return *this;
}
return *this;
}
//how to make new map that contains two old maps
Posiljka Posiljka::operator|(Posiljka &p){
Posiljka novi;
map<string,int>::iterator it;
for( it = p.m.begin() ; it!= p.m.end() ; ++it)
novi.m[it->first]=it->second;
for( it = m.begin(); it != m.end() ; ++it){
if(novi.m[it->first])
novi.m[it->first] = novi.m[it->first] + it->second;
else
novi.m[it->first] = it->second;
}
return novi;
}
//multiply map witn int and make new map
Posiljka Posiljka::operator*(int x){
map<string , int>::iterator it;
Posiljka novi;
for( it=m.begin() ; it!=m.end() ; ++it)
novi.m[it->first] = it->second * x;
return novi;
}
//write how much objects map has
Posiljka::operator int(){
int suma=0;
map<string , int>::iterator it;
for( it = m.begin() ; it != m.end() ; ++it)
suma = suma + it->second;
return (int)suma;
}
//write maks of one object
int Posiljka::operator+(){
int maks=0;
map<string, int>::iterator it;
for( it = m.begin() ; it != m.end() ; ++it)
if( it->second > maks)
maks=it->second;
return maks;
}
//write min of one object
int Posiljka::operator-(){
if(m.begin()->second)
return 0;
int mini=m.begin()->second;
map<string, int>::iterator it;
for( it=m.begin() ; it!=m.end() ; ++it)
if( it->second < mini)
mini=it->second;
return mini;
}
//if there is object s in thios map
bool Posiljka::operator()(string s){
if(m[s])
return true;
else
return false;
}
我的主要:
#include <iostream>
#include "druga_verzija.h"
using namespace std;
int main()
{
Posiljka P, Q, R;
P << "olovka" << "tipkovnica" << "olovka" << "olovka";
cout << P << endl;
P << "olovka" << "monitor" << "tipkovnica" << "gitara";
cout << P << endl;
//1xgitara 1xmonitor 4xolovka 2xtipkovnica
cout << (int)P << endl;
//8
Q = P*2;
cout << Q << endl;
//2xgitara 2xmonitor 8xolovka 4xtipkovnica
R = P | Q;
cout << R << endl;
//3xgitara 3xmonitor 12xolovka 6xtipkovnica
cout << -R << " " << +R << endl;
//3 12
while ( R("olovka") )
R >> "olovka";
cout << R << endl;
//3xgitara 3xmonitor 6xtipkovnica
R >> "gitara" >> "monitor" >> "tipkovnica" >> "tipkovnica";
cout << R << endl;
//2xgitara 2xmonitor 4xtipkovnica
return 0;
}
我的界面:
#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
using namespace std;
class Posiljka
{
private:
map<string,int> m;
public:
Posiljka();
friend ostream&operator<<(ostream&, Posiljka&);
Posiljka&operator<<(string s);
Posiljka&operator>>(string s);
Posiljka operator|(Posiljka &p);
Posiljka operator*(int x);
operator int();
int operator+();
int operator-();
bool operator()(string s);
};
一个问题是您没有从声明为 return 值的函数中 returning 值:
ostream&operator<<(ostream&f, Posiljka&p)
{
map<string , int>::iterator it;
it=p.m.begin();
cout << it->second << "x" << it->first;
for( it=p.m.begin() ; it != p.m.end() ; ++it)
{
cout << " " << it->second << "x" << it->first;
}
// Where is the return????
}
不return从声明为return的函数中获取值是未定义的行为。该功能可能应该是:
ostream&operator<<(ostream &f, const Posiljka &p)
{
map<string , int>::const_iterator it;
it=p.m.begin();
f << it->second << "x" << it->first;
for( it=p.m.begin() ; it != p.m.end() ; ++it)
{
f << " " << it->second << "x" << it->first;
}
return f;
}
1) 你的输出流是f
,不应该是cout
。
2) 你应该通过 const reference
.
通过 Posiljka
3) 注意现在我们 return f
.
这个函数可能遇到同样的问题:
Posiljka&Posiljka::operator<<(string s)
在该函数中,您确实有 return
语句,但并非所有路径 return 都有值。
我一直在为我的 class 作业编写代码,当我使用 g++ 命令在终端中运行它时,它给我分段错误。代码很大,但第一部分,当我插入地图元素时,它可以工作(因为一旦它编写了命令然后被粉碎)然后当它必须转换时(这是使用重新定义运算符的赋值)它崩溃了。有人可以帮助我吗?
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <typeinfo>
#include "druga_verzija.h"
using namespace std;
//constructor
Posiljka::Posiljka(){
m.clear();
}
//how to print my map
ostream&operator<<(ostream&f, Posiljka&p){
map<string , int>::iterator it;
it=p.m.begin();
cout << it->second << "x" << it->first;
for( it=p.m.begin() ; it != p.m.end() ; ++it){
cout << " " << it->second << "x" << it->first;
}
}
//how to insert elements
Posiljka&Posiljka::operator<<(string s){
if(m.empty()){
m.insert(pair<string,int>(s,1));
return *this;
}
map<string ,int>::iterator it=m.begin();
for(; it != m.end() ; ++it){
if( it->first == s){
it->second++;
return *this;
}
if( it->first > s){
m.insert( it, pair<string,int>(s , 1));
return *this;
}
}
if(it == m.end()){
m.insert(pair<string,int>( s, 1));
return *this;
}
}
//how to delete them
Posiljka&Posiljka::operator>>(string s){
if(m[s]){
m[s]=m[s]-1;
if(!m[s]){
m.erase(s);
return *this;
}
return *this;
}
return *this;
}
//how to make new map that contains two old maps
Posiljka Posiljka::operator|(Posiljka &p){
Posiljka novi;
map<string,int>::iterator it;
for( it = p.m.begin() ; it!= p.m.end() ; ++it)
novi.m[it->first]=it->second;
for( it = m.begin(); it != m.end() ; ++it){
if(novi.m[it->first])
novi.m[it->first] = novi.m[it->first] + it->second;
else
novi.m[it->first] = it->second;
}
return novi;
}
//multiply map witn int and make new map
Posiljka Posiljka::operator*(int x){
map<string , int>::iterator it;
Posiljka novi;
for( it=m.begin() ; it!=m.end() ; ++it)
novi.m[it->first] = it->second * x;
return novi;
}
//write how much objects map has
Posiljka::operator int(){
int suma=0;
map<string , int>::iterator it;
for( it = m.begin() ; it != m.end() ; ++it)
suma = suma + it->second;
return (int)suma;
}
//write maks of one object
int Posiljka::operator+(){
int maks=0;
map<string, int>::iterator it;
for( it = m.begin() ; it != m.end() ; ++it)
if( it->second > maks)
maks=it->second;
return maks;
}
//write min of one object
int Posiljka::operator-(){
if(m.begin()->second)
return 0;
int mini=m.begin()->second;
map<string, int>::iterator it;
for( it=m.begin() ; it!=m.end() ; ++it)
if( it->second < mini)
mini=it->second;
return mini;
}
//if there is object s in thios map
bool Posiljka::operator()(string s){
if(m[s])
return true;
else
return false;
}
我的主要:
#include <iostream>
#include "druga_verzija.h"
using namespace std;
int main()
{
Posiljka P, Q, R;
P << "olovka" << "tipkovnica" << "olovka" << "olovka";
cout << P << endl;
P << "olovka" << "monitor" << "tipkovnica" << "gitara";
cout << P << endl;
//1xgitara 1xmonitor 4xolovka 2xtipkovnica
cout << (int)P << endl;
//8
Q = P*2;
cout << Q << endl;
//2xgitara 2xmonitor 8xolovka 4xtipkovnica
R = P | Q;
cout << R << endl;
//3xgitara 3xmonitor 12xolovka 6xtipkovnica
cout << -R << " " << +R << endl;
//3 12
while ( R("olovka") )
R >> "olovka";
cout << R << endl;
//3xgitara 3xmonitor 6xtipkovnica
R >> "gitara" >> "monitor" >> "tipkovnica" >> "tipkovnica";
cout << R << endl;
//2xgitara 2xmonitor 4xtipkovnica
return 0;
}
我的界面:
#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
using namespace std;
class Posiljka
{
private:
map<string,int> m;
public:
Posiljka();
friend ostream&operator<<(ostream&, Posiljka&);
Posiljka&operator<<(string s);
Posiljka&operator>>(string s);
Posiljka operator|(Posiljka &p);
Posiljka operator*(int x);
operator int();
int operator+();
int operator-();
bool operator()(string s);
};
一个问题是您没有从声明为 return 值的函数中 returning 值:
ostream&operator<<(ostream&f, Posiljka&p)
{
map<string , int>::iterator it;
it=p.m.begin();
cout << it->second << "x" << it->first;
for( it=p.m.begin() ; it != p.m.end() ; ++it)
{
cout << " " << it->second << "x" << it->first;
}
// Where is the return????
}
不return从声明为return的函数中获取值是未定义的行为。该功能可能应该是:
ostream&operator<<(ostream &f, const Posiljka &p)
{
map<string , int>::const_iterator it;
it=p.m.begin();
f << it->second << "x" << it->first;
for( it=p.m.begin() ; it != p.m.end() ; ++it)
{
f << " " << it->second << "x" << it->first;
}
return f;
}
1) 你的输出流是f
,不应该是cout
。
2) 你应该通过 const reference
.
3) 注意现在我们 return f
.
这个函数可能遇到同样的问题:
Posiljka&Posiljka::operator<<(string s)
在该函数中,您确实有 return
语句,但并非所有路径 return 都有值。