求救啊!模板类能嵌套的吧,为什么错误 10
-------------#include<iostream>usingnamespacestd;classmystring{charstring[20];intmaxs...
-------------
#include<iostream>
using namespace std;
class mystring{
char string[20];int maxsize2;
int last2;
public:
mystring (){last2=-1;maxsize2=20;
string[0]='\0';}
mystring(char*s){
last2=-1;maxsize2=20;
do{
last2++;string[last2]=s[last2];}while (s[last2]!='\0'&&last2<maxsize2-1);
string[last2]='\0';
}
~mystring(){}
void show(){cout<<string<<'\t';}
void putdata(char*);
bool operator<(mystring &);};
bool mystring::operator < (mystring&s){
int i;
for(i=0;i<last2&&i<s.last2;i++){
if(string[i]<s.string [i])return true;
if(string[i]>s.string [i])return false;
}
if(last2<s.last2)return true;
else return false;
}
void mystring ::putdata(char*k){
int i;
for(i=0;i<last2&&k[i]!='\0';i++)string[i]=k[i];
string[i]='\0';}
template<typename t>class node{
t key;
public:
operator<(node<t>&);};
template<typename t>bool node<t>::operator<(node<t>& l){
return key<l.key;}
template<typename t,int size>class orderedlist{
node <t>slist[size];
int last;
int maxsize;
public:
orderedlist(){last=-1;maxsize=size;}
void selectsort();
bool insert(node<t>temp;int i);
void print();
};
template<typename t,int size>bool orderedlist<t,size>::insert (node<t>elem,int i){
if(last==maxsize-1||i<0||i>last+1){cout<<"error";return false;}
last++;
int j;
for(j=last;j>i;j--)slist[j]=slist[j-1];
slist[i]=elem;return true;}
template<typename t,int size>void orderedlist<t,size>::print(){
int i;
for(i=0;i<=last;i++)slist[i].key.show();}
template<typename t,int size>void orderedlist<t,size>::selectsort(){
int i,j,k;
node<t>temp;
for(i=0;i<last;i++){
k=i;
temp=slist[i];
for(j=i;j<=last;j++)
if(slist[j]<temp){k=j;temp=slist[j];}
if(k!=i){temp=slist[i];
slist[i]=slist[k]
slist[k]=temp;}
}
}
int main(){
const int h=10;
int i;
orderedlist<mystring ,100>ordlist;
char mslist[h]={"wuhao","zhongjian","jiumei","guoyong","jilin","zhiyu","wo","bowang","zhenyin","qihui"};
node<mystring>n[h];
for(i=0;i<h;i++)n[i].key.putdata(mslist[i]);
for(i=0;i<h;i++)ordlist.insert(n[i],i);
cout<<"未排序表:"<<endl;
ordlist.print();
ordlist.selectsort();
cout<<"已排序表:"<<endl;
ordlist.print();
return 0;}
- 展开
#include<iostream>
using namespace std;
class mystring{
char string[20];int maxsize2;
int last2;
public:
mystring (){last2=-1;maxsize2=20;
string[0]='\0';}
mystring(char*s){
last2=-1;maxsize2=20;
do{
last2++;string[last2]=s[last2];}while (s[last2]!='\0'&&last2<maxsize2-1);
string[last2]='\0';
}
~mystring(){}
void show(){cout<<string<<'\t';}
void putdata(char*);
bool operator<(mystring &);};
bool mystring::operator < (mystring&s){
int i;
for(i=0;i<last2&&i<s.last2;i++){
if(string[i]<s.string [i])return true;
if(string[i]>s.string [i])return false;
}
if(last2<s.last2)return true;
else return false;
}
void mystring ::putdata(char*k){
int i;
for(i=0;i<last2&&k[i]!='\0';i++)string[i]=k[i];
string[i]='\0';}
template<typename t>class node{
t key;
public:
operator<(node<t>&);};
template<typename t>bool node<t>::operator<(node<t>& l){
return key<l.key;}
template<typename t,int size>class orderedlist{
node <t>slist[size];
int last;
int maxsize;
public:
orderedlist(){last=-1;maxsize=size;}
void selectsort();
bool insert(node<t>temp;int i);
void print();
};
template<typename t,int size>bool orderedlist<t,size>::insert (node<t>elem,int i){
if(last==maxsize-1||i<0||i>last+1){cout<<"error";return false;}
last++;
int j;
for(j=last;j>i;j--)slist[j]=slist[j-1];
slist[i]=elem;return true;}
template<typename t,int size>void orderedlist<t,size>::print(){
int i;
for(i=0;i<=last;i++)slist[i].key.show();}
template<typename t,int size>void orderedlist<t,size>::selectsort(){
int i,j,k;
node<t>temp;
for(i=0;i<last;i++){
k=i;
temp=slist[i];
for(j=i;j<=last;j++)
if(slist[j]<temp){k=j;temp=slist[j];}
if(k!=i){temp=slist[i];
slist[i]=slist[k]
slist[k]=temp;}
}
}
int main(){
const int h=10;
int i;
orderedlist<mystring ,100>ordlist;
char mslist[h]={"wuhao","zhongjian","jiumei","guoyong","jilin","zhiyu","wo","bowang","zhenyin","qihui"};
node<mystring>n[h];
for(i=0;i<h;i++)n[i].key.putdata(mslist[i]);
for(i=0;i<h;i++)ordlist.insert(n[i],i);
cout<<"未排序表:"<<endl;
ordlist.print();
ordlist.selectsort();
cout<<"已排序表:"<<endl;
ordlist.print();
return 0;}
- 展开
1个回答
展开全部
已修改正确:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class mystring
{
char string[20];
int maxsize2;
int last2;
public:
mystring()
{
last2 = -1;
maxsize2 = 20;
string[0] = '\0';
}
mystring (char *s)
{
last2 = -1;
maxsize2 = 20;
do
{
last2++;
string[last2] = s[last2];
} while(s[last2] != '\0' && last2 < maxsize2 - 1);
string[last2] = '\0';
}
~mystring() { }
void show ()
{
printf("mystring_%p, last2:%2d ", this, last2); //++
cout << string << '\n';
}
void putdata (char *);
bool operator < (mystring &);
};
bool mystring::operator < (mystring & s)
{
#if 1
int i;
// for(i = 0; i < last2 && i < s.last2; i++) // implict BUG.
for(i = 0; i <= last2 && i <= s.last2; i++)
{
if(string[i] < s.string[i]) return true;
if(string[i] > s.string[i]) return false;
}
if(last2 < s.last2) return true;
else
return false;
#else
return strcmp(string, s.string) < 0;
#endif
}
void mystring::putdata (char *k)
{
int i;
// for(i = 0; i < last2 && k[i] != '\0'; i++) string[i] = k[i];
// FATAL BUG: last2 初始化为 -1.
// 所以 main 里 n[i].key.putdata(mslist[i]); 调用时 n[i].key.string[] 不会被正确的设置
for(i = 0; i < maxsize2 && k[i] != '\0'; i++) string[i] = k[i]; // modified
string[i] = '\0';
last2 = i-1;
}
template < typename t >
// class node
struct node // modified: to fix "ERROR: key是私有成员"
{
t key;
public:
// operator < (node<t> &); // ERROR: 没有返回值类型
bool operator < (node<t> &); // modified
};
template < typename t >
bool node<t>::operator<(node<t> &l)
{
return key < l.key;
}
template<typename t, int size>
class orderedlist
{
node<t> slist[size];
int last;
int maxsize;
public:
orderedlist()
{
last = -1;
maxsize = size;
}
void selectsort();
// bool insert(node<t> temp; int i); // ERROR: 中间应该是 ,
bool insert(node<t> temp, int i); // modified
void print();
};
template<typename t, int size>
bool orderedlist<t, size>::insert(node<t> elem, int i)
{
if(last == maxsize - 1 || i < 0 || i > last + 1)
{
cout << "error";
return false;
}
last++;
int j;
for(j = last; j > i; j--) slist[j] = slist[j - 1];
slist[i] = elem;
return true;
}
template<typename t, int size>
void orderedlist<t, size>::print()
{
int i;
printf("list_%p, last: %d\n", this, last); // ++
for(i = 0; i <= last; i++) slist[i].key.show();
}
template<typename t, int size>
void orderedlist<t, size>::selectsort()
{
int i, j, k;
node<t> temp;
#if 0
for(i = 0; i < last; i++)
{
k = i;
temp = slist[i];
for(j = i; j <= last; j++)
if(slist[j] < temp)
{
k = j;
temp = slist[j];
}
if(k != i)
{
temp = slist[i];
// slist[i] = slist[k]slist[k] = temp; // ERROR: 缺少分号
slist[i] = slist[k]; // modified
slist[k] = temp;
}
}
#else
// 以上代码可以简化为:
for(i = 0; i < last; i++)
{
k = i;
for(j = i+1; j <= last; j++)
if(slist[j] < slist[k]) k = j;
if(k != i) {
temp = slist[i];
slist[i] = slist[k];
slist[k] = temp;
}
}
#endif
}
int main ()
{
const int h = 10;
int i;
orderedlist<mystring, 100> ordlist;
// char mslist[h] = // ERROR
char *mslist[h] = // modified
{
"wuhao",
"zhongjian",
"jiumei",
"guoyong",
"jilin",
"zhiyu",
"wo",
"bowang",
"zhenyin",
"qihui"
};
node<mystring> n[h];
// for(i = 0; i < h; i++) n[i].key.putdata(mslist[i]); // ERROR: key是私有成员,
// 可以将node的声明由class改为struct(二者在C++中仅默认访问权限不同,struct默认public)
for(i = 0; i < h; i++) {
n[i].key.putdata(mslist[i]);
n[i].key.show();
}
for(i = 0; i < h; i++) ordlist.insert(n[i], i);
cout << "\n未排序表:" << endl;
ordlist.print();
ordlist.selectsort();
cout << "\n已排序表:" << endl;
ordlist.print();
return 0;
}
这里用到了
#if 0
#else
#endif
可以非常方便的进行代码切换(只需修改0/1),这和《编程珠玑》里所说的“脚手架方法”非常相似。
另外,对于实现一个数据结构,应该先写出证算法正确的C代码,再考虑封装为类、类模板,详见我的一篇博文:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询