写两个类,同一个程序,为什么Devc++能运行,visual studio 2010控制台在运行时提示string subscript out of range

qq_34600946 2016-05-10 08:23:23
如题,代码如下

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <utility>
using namespace std;

class Keyword
{
protected:
int wordtype; //名词为 1,动词为 2,形容词为 3,虚词为 4
string word; //关键词的内容
int wordsize; //关键词的字数
string pinyin;
int *pingze; //韵母种类是指根据押韵规律将相近的韵母归为一类
int yunmutype; //韵母指的是最后一个字的韵母

public:


Keyword(){};

int getyunmutype()
{
return(yunmutype);
}

string getword()
{
return(word);
}

int getwordsize()
{
return(wordsize);
}

int *getpingze()
{
return(pingze);
}

string getpinyin()
{
return(pinyin);
}

int getwordtype()
{
return(wordtype);
}

Keyword(int wordtype,string word,string pinyin)
{
int k=0;
string yunmuyun="";
this->wordtype=wordtype;
this->word=word;
this->pinyin=pinyin;
this->wordsize=word.length()/2;
pingze=new int[wordsize];
for(int i=0;i<wordsize;i++)
pingze[i]=0;
for(int i=0 ; pinyin[i] ; i++)
{
if(pinyin[i]=='1'||pinyin[i]=='2')
pingze[k++]=-1;
if(pinyin[i]=='3'||pinyin[i]=='4')
pingze[k++]=1;
}

char ptr[20]={0};
int kk=0;

for(int i=0; pinyin[kk] ;i++,kk++)
{
if(pinyin[kk]>='1'&&pinyin[kk]<='4')
kk++;
ptr[i]=pinyin[kk];
}

string ppinyin(ptr);

int len=ppinyin.length();

if(ppinyin[len-1]=='a'||
ppinyin[len-1]=='o'||
ppinyin[len-1]=='e'||
ppinyin[len-1]=='i'||
ppinyin[len-1]=='u'||
ppinyin[len-1]=='v')
yunmuyun+=ppinyin[len-1];

if(((ppinyin[len-1]=='i')&&(ppinyin[len-2]=='a'||ppinyin[len-2]=='e'||ppinyin[len-2]=='u'))
||((ppinyin[len-1]=='o')&&(ppinyin[len-2]=='a'))
||((ppinyin[len-1]=='u')&&(ppinyin[len-2]=='o'||ppinyin[len-2]=='i'))
||((ppinyin[len-1]=='e')&&(ppinyin[len-2]=='i'||ppinyin[len-2]=='v'))
||((ppinyin[len-1]=='r')&&(ppinyin[len-2]=='e'))
||((ppinyin[len-1]=='n')&&(ppinyin[len-2]=='a'||ppinyin[len-2]=='e'||ppinyin[len-2]=='u'||ppinyin[len-2]=='v'||ppinyin[len-2]=='i')))
{
yunmuyun+=ppinyin[len-2];
yunmuyun+=ppinyin[len-1];
}

if((ppinyin[len-1]=='g')&&(ppinyin[len-2]=='n')&&(ppinyin[len-3]=='a'||ppinyin[len-3]=='e'||ppinyin[len-3]=='o'||ppinyin[len-3]=='i'))
{
yunmuyun+=ppinyin[len-3];
yunmuyun+=ppinyin[len-2];
yunmuyun+=ppinyin[len-1];
}

if(yunmuyun == "a")
yunmutype=1;
if (yunmuyun == "ai")
yunmutype=2;
if(yunmuyun == "an")
yunmutype=3;
if (yunmuyun == "ang")
yunmutype=4;
if(yunmuyun == "ao")
yunmutype=5;
if (yunmuyun=="e"||yunmuyun=="o")
yunmutype=6;
if(yunmuyun=="ei"||yunmuyun=="ui")
yunmutype=7;
if ((yunmuyun=="en")||(yunmuyun=="in")||(yunmuyun=="un")||(yunmuyun=="vn"))
yunmutype=8;
if ((yunmuyun=="eng")||(yunmuyun=="ing")||(yunmuyun=="ong"))
yunmutype=9;
if ((yunmuyun=="i"||yunmuyun=="er"))
yunmutype=10;
if(yunmuyun=="ie")
yunmutype=11;
if (yunmuyun=="ou"||yunmuyun=="iu")
yunmutype=12;
if(yunmuyun=="u")
yunmutype=13;
if (yunmuyun=="v")
yunmutype=14;
if(yunmuyun=="ve")
yunmutype=15;
}; //构造函数
//平仄规定:平为 -1,仄为 1

~Keyword(){}; //析构函数

bool operator== (const Keyword &I2)
{
if(this->wordtype==I2.wordtype&&
this->word==I2.word&&
this->wordsize==I2.wordsize&&
this->pinyin==I2.pinyin&&
this->pingze==I2.pingze&&
this->yunmutype==I2.yunmutype)
return(1);
else return(0);
}



void operator= (const Keyword &K2)
{
this->wordtype=K2.wordtype;
this->word=K2.word;
this->wordsize=K2.wordsize;
this->pinyin=K2.pinyin;
this->pingze=K2.pingze;
this->yunmutype=K2.yunmutype;
}

bool operator!= (const Keyword &K2)
{
if(this->wordtype!=K2.wordtype||
this->word!=K2.word||
this->wordsize!=K2.wordsize||
this->pinyin!=K2.pinyin||
this->pingze!=K2.pingze||
this->yunmutype!=K2.yunmutype)
return(1);
else
return(0);
}

Keyword(const Keyword& K):wordtype(K.wordtype),word(K.word),
wordsize(K.wordsize),pinyin(K.pinyin),pingze(K.pingze)
,yunmutype(K.yunmutype){}; //复制构造函数

void getall()
{
cout<<wordtype<<endl;
cout<<word<<endl;
cout<<wordsize<<endl;

for(int i=0;i<wordsize;i++)
cout<<pingze[i]<<' ';

cout<<endl;
cout<<yunmutype<<endl;

}
};



//关键词类定义完毕,以下是派生类:名词,意象,动词,虚词的定义
//名词 1,动词 2,形容词 3,虚词 4

class image: public Keyword
{
public:
image(int wordtype,string word,string pinyin,int emotion,int style)
:Keyword(1,word,pinyin),emotion(emotion),style(style){};

image(){};


void operator= (const image &I2)
{
this->wordtype=I2.wordtype;
this->word=I2.word;
this->wordsize=I2.wordsize;
this->pinyin=I2.pinyin;
this->yunmutype=I2.yunmutype;
this->pingze=I2.pingze;
this->emotion=I2.emotion;
this->style=I2.style;
}



image(const image& I):Keyword(1,I.word,I.pinyin),
emotion(I.emotion),style(I.style){};

~image(){};
private:
int emotion;
int style;
};



int main()
{
Keyword K(1,"我爱你","wo3ai4ni3");
K.getall();
return 0;
}


感谢不尽
...全文
148 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqbk1 2016-05-11
  • 打赏
  • 举报
回复
	Keyword(int wordtype,string word,string pinyin)
	{
		int k=0;
		string yunmuyun="";
		this->wordtype=wordtype;
		this->word=word;
		this->pinyin=pinyin;
		this->wordsize=word.length()/2;
		pingze=new int[wordsize];
		for(int i=0;i<wordsize;i++)
			pingze[i]=0;
		
		for(int i=0 ; i< pinyin.length(); i++)
		{
			if(pinyin[i]=='1'||pinyin[i]=='2')
				pingze[k++]=-1;
			if(pinyin[i]=='3'||pinyin[i]=='4')
				pingze[k++]=1;	
		}

		char ptr[20]={0};
		int kk=0;
		for(int i=0; kk<pinyin.length() ;i++,kk++)
		{
			if(pinyin[kk]>='1'&&pinyin[kk]<='4')
				kk++;
			ptr[i]=pinyin[kk];
		}
		
		string ppinyin(ptr);

		int len=ppinyin.length();	

		if(ppinyin[len-1]=='a'||
			ppinyin[len-1]=='o'||
			ppinyin[len-1]=='e'||
			ppinyin[len-1]=='i'||
			ppinyin[len-1]=='u'||
			ppinyin[len-1]=='v')
			yunmuyun+=ppinyin[len-1];

		if(((ppinyin[len-1]=='i')&&(ppinyin[len-2]=='a'||ppinyin[len-2]=='e'||ppinyin[len-2]=='u'))
			||((ppinyin[len-1]=='o')&&(ppinyin[len-2]=='a'))
			||((ppinyin[len-1]=='u')&&(ppinyin[len-2]=='o'||ppinyin[len-2]=='i'))
			||((ppinyin[len-1]=='e')&&(ppinyin[len-2]=='i'||ppinyin[len-2]=='v'))
			||((ppinyin[len-1]=='r')&&(ppinyin[len-2]=='e'))
			||((ppinyin[len-1]=='n')&&(ppinyin[len-2]=='a'||ppinyin[len-2]=='e'||ppinyin[len-2]=='u'||ppinyin[len-2]=='v'||ppinyin[len-2]=='i')))	
		{
			yunmuyun+=ppinyin[len-2];
			yunmuyun+=ppinyin[len-1];
		}

		if((ppinyin[len-1]=='g')&&(ppinyin[len-2]=='n')&&(ppinyin[len-3]=='a'||ppinyin[len-3]=='e'||ppinyin[len-3]=='o'||ppinyin[len-3]=='i'))
		{
			yunmuyun+=ppinyin[len-3];
			yunmuyun+=ppinyin[len-2];
			yunmuyun+=ppinyin[len-1];
		}

		if(yunmuyun == "a")
			yunmutype=1;
		if (yunmuyun == "ai")
			yunmutype=2;
		if(yunmuyun == "an")
			yunmutype=3;
		if (yunmuyun == "ang")
			yunmutype=4;
		if(yunmuyun == "ao")
			yunmutype=5;
		if (yunmuyun=="e"||yunmuyun=="o")
			yunmutype=6;
		if(yunmuyun=="ei"||yunmuyun=="ui")
			yunmutype=7;
		if ((yunmuyun=="en")||(yunmuyun=="in")||(yunmuyun=="un")||(yunmuyun=="vn"))
			yunmutype=8;
		if ((yunmuyun=="eng")||(yunmuyun=="ing")||(yunmuyun=="ong"))
			yunmutype=9;
		if ((yunmuyun=="i"||yunmuyun=="er"))
			yunmutype=10;
		if(yunmuyun=="ie")
			yunmutype=11;
		if (yunmuyun=="ou"||yunmuyun=="iu")
			yunmutype=12;
		if(yunmuyun=="u")
			yunmutype=13;
		if (yunmuyun=="v")
			yunmutype=14;
		if(yunmuyun=="ve")
			yunmutype=15;	
	};
lqbk1 2016-05-11
  • 打赏
  • 举报
回复
引用 3 楼 qq_34600946 的回复:
不行啊,还是这样
下面这段也是要改的,按2楼的套路请自己改改吧

 for(int i=0 ; pinyin[i] ; i++)
        {
            if(pinyin[i]=='1'||pinyin[i]=='2')
                pingze[k++]=-1;
            if(pinyin[i]=='3'||pinyin[i]=='4')
                pingze[k++]=1;   
        }
qq_34600946 2016-05-11
  • 打赏
  • 举报
回复
行了,真的是这个问题,O(∩_∩)O谢谢
lqbk1 2016-05-11
  • 打赏
  • 举报
回复
再修一下,只贴重要部分... .

Keyword(int wordtype,string word,string pinyin)
	{
		int k=0;
		string yunmuyun="";
		this->wordtype=wordtype;
		this->word=word;
		this->pinyin=pinyin;
		this->wordsize=word.length()/2;
		pingze=new int[wordsize];
		for(int i=0;i<wordsize;i++)
			pingze[i]=0;
		
		for(int i=0 ; i< pinyin.length(); i++)
		{
			if(pinyin[i]=='1'||pinyin[i]=='2')
				pingze[k++]=-1;
			if(pinyin[i]=='3'||pinyin[i]=='4')
				pingze[k++]=1;	
		}

		char ptr[20]={0};
		int kk=0;
		for(int i=0; kk<pinyin.length() ;i++,kk++)
		{
			if(pinyin[kk]>='1'&&pinyin[kk]<='4')
				kk++;
			if (kk>=pinyin.length()){
				break;
			}
			ptr[i]=pinyin[kk];
		}
//.............		
 
qq_34600946 2016-05-10
  • 打赏
  • 举报
回复
不行啊,还是这样
lqbk1 2016-05-10
  • 打赏
  • 举报
回复
// bmp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>      
#include <cstdlib> 
#include <ctime> 
#include <vector>
#include <string>
#include <map>
#include <utility>
using namespace std;

class Keyword                  
{
protected:
	int wordtype;        //名词为 1,动词为 2,形容词为 3,虚词为 4   
	string word;        //关键词的内容             
	int wordsize;   	//关键词的字数
	string pinyin;      
	int *pingze;	            //韵母种类是指根据押韵规律将相近的韵母归为一类
	int yunmutype;	    //韵母指的是最后一个字的韵母

public:	


	Keyword(){};

	int getyunmutype()
	{
		return(yunmutype);
	}

	string getword()
	{
		return(word);	
	}	

	int getwordsize()
	{
		return(wordsize);	
	}	

	int *getpingze()	
	{
		return(pingze);
	}

	string getpinyin()
	{
		return(pinyin);	
	}	

	int getwordtype()
	{
		return(wordtype);	
	}	

	Keyword(int wordtype,string word,string pinyin)
	{
		int k=0;
		string yunmuyun="";
		this->wordtype=wordtype;
		this->word=word;
		this->pinyin=pinyin;
		this->wordsize=word.length()/2;
		pingze=new int[wordsize];
		for(int i=0;i<wordsize;i++)
			pingze[i]=0;
		for(int i=0 ; pinyin[i] ; i++)
		{
			if(pinyin[i]=='1'||pinyin[i]=='2')
				pingze[k++]=-1;
			if(pinyin[i]=='3'||pinyin[i]=='4')
				pingze[k++]=1;	
		}

		char ptr[20]={0};
		int kk=0;
		//请看这里 string内部的实现中 不保证字符串后面有'0',其长度为len的话,用下标只能访问0到len-1
		char const *str = pinyin.c_str();
		for(int i=0; str[kk] ;i++,kk++)
		{
			if(str[kk]>='1'&&str[kk]<='4')
				kk++;
			ptr[i]=str[kk];
		}
		//可以照上面那样改 也可以写出这样
		/*for(int i=0; kk<pinyin.length() ;i++,kk++)
		{
			if(pinyin[kk]>='1'&&pinyin[kk]<='4')
				kk++;
			ptr[i]=pinyin[kk];
		}*/
		
		
		string ppinyin(ptr);

		int len=ppinyin.length();	

		if(ppinyin[len-1]=='a'||
			ppinyin[len-1]=='o'||
			ppinyin[len-1]=='e'||
			ppinyin[len-1]=='i'||
			ppinyin[len-1]=='u'||
			ppinyin[len-1]=='v')
			yunmuyun+=ppinyin[len-1];

		if(((ppinyin[len-1]=='i')&&(ppinyin[len-2]=='a'||ppinyin[len-2]=='e'||ppinyin[len-2]=='u'))
			||((ppinyin[len-1]=='o')&&(ppinyin[len-2]=='a'))
			||((ppinyin[len-1]=='u')&&(ppinyin[len-2]=='o'||ppinyin[len-2]=='i'))
			||((ppinyin[len-1]=='e')&&(ppinyin[len-2]=='i'||ppinyin[len-2]=='v'))
			||((ppinyin[len-1]=='r')&&(ppinyin[len-2]=='e'))
			||((ppinyin[len-1]=='n')&&(ppinyin[len-2]=='a'||ppinyin[len-2]=='e'||ppinyin[len-2]=='u'||ppinyin[len-2]=='v'||ppinyin[len-2]=='i')))	
		{
			yunmuyun+=ppinyin[len-2];
			yunmuyun+=ppinyin[len-1];
		}

		if((ppinyin[len-1]=='g')&&(ppinyin[len-2]=='n')&&(ppinyin[len-3]=='a'||ppinyin[len-3]=='e'||ppinyin[len-3]=='o'||ppinyin[len-3]=='i'))
		{
			yunmuyun+=ppinyin[len-3];
			yunmuyun+=ppinyin[len-2];
			yunmuyun+=ppinyin[len-1];
		}

		if(yunmuyun == "a")
			yunmutype=1;
		if (yunmuyun == "ai")
			yunmutype=2;
		if(yunmuyun == "an")
			yunmutype=3;
		if (yunmuyun == "ang")
			yunmutype=4;
		if(yunmuyun == "ao")
			yunmutype=5;
		if (yunmuyun=="e"||yunmuyun=="o")
			yunmutype=6;
		if(yunmuyun=="ei"||yunmuyun=="ui")
			yunmutype=7;
		if ((yunmuyun=="en")||(yunmuyun=="in")||(yunmuyun=="un")||(yunmuyun=="vn"))
			yunmutype=8;
		if ((yunmuyun=="eng")||(yunmuyun=="ing")||(yunmuyun=="ong"))
			yunmutype=9;
		if ((yunmuyun=="i"||yunmuyun=="er"))
			yunmutype=10;
		if(yunmuyun=="ie")
			yunmutype=11;
		if (yunmuyun=="ou"||yunmuyun=="iu")
			yunmutype=12;
		if(yunmuyun=="u")
			yunmutype=13;
		if (yunmuyun=="v")
			yunmutype=14;
		if(yunmuyun=="ve")
			yunmutype=15;	
	}; //构造函数
	//平仄规定:平为 -1,仄为 1

	~Keyword(){};                //析构函数

	bool operator== (const Keyword &I2) 
	{
		if(this->wordtype==I2.wordtype&&
			this->word==I2.word&&
			this->wordsize==I2.wordsize&&
			this->pinyin==I2.pinyin&&
			this->pingze==I2.pingze&&
			this->yunmutype==I2.yunmutype)
			return(1);
		else return(0);	
	} 



	void operator= (const Keyword &K2) 
	{
		this->wordtype=K2.wordtype;
		this->word=K2.word;
		this->wordsize=K2.wordsize;
		this->pinyin=K2.pinyin;	
		this->pingze=K2.pingze;
		this->yunmutype=K2.yunmutype;
	} 

	bool operator!= (const Keyword &K2) 
	{
		if(this->wordtype!=K2.wordtype||
			this->word!=K2.word||
			this->wordsize!=K2.wordsize||
			this->pinyin!=K2.pinyin||
			this->pingze!=K2.pingze||
			this->yunmutype!=K2.yunmutype)
			return(1);
		else 
			return(0);	
	} 

	Keyword(const Keyword& K):wordtype(K.wordtype),word(K.word),
		wordsize(K.wordsize),pinyin(K.pinyin),pingze(K.pingze)
		,yunmutype(K.yunmutype){};   //复制构造函数

	void getall()
	{
		cout<<wordtype<<endl;
		cout<<word<<endl;
		cout<<wordsize<<endl;

		for(int i=0;i<wordsize;i++)
			cout<<pingze[i]<<' ';

		cout<<endl;	
		cout<<yunmutype<<endl;

	}
};



//关键词类定义完毕,以下是派生类:名词,意象,动词,虚词的定义
//名词 1,动词 2,形容词 3,虚词 4

class image: public Keyword
{
public:
	image(int wordtype,string word,string pinyin,int emotion,int style)
		:Keyword(1,word,pinyin),emotion(emotion),style(style){};

	image(){};


	void operator= (const image &I2) 
	{
		this->wordtype=I2.wordtype;
		this->word=I2.word;
		this->wordsize=I2.wordsize;
		this->pinyin=I2.pinyin;
		this->yunmutype=I2.yunmutype;	
		this->pingze=I2.pingze;
		this->emotion=I2.emotion;
		this->style=I2.style;
	} 



	image(const image& I):Keyword(1,I.word,I.pinyin),
		emotion(I.emotion),style(I.style){};

	~image(){};
private:
	int emotion;
	int style;
};



int main()
{
	Keyword K(1,"我爱你","wo3ai4ni3");
	K.getall();
	return 0;
}
qq_34600946 2016-05-10
  • 打赏
  • 举报
回复
把 image 类那部分去掉也没有用

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧