注册 登录
编程论坛 C++教室

新学C++,有个问题

bestcln 发布于 2008-03-23 19:38, 1352 次点击
才开始看C++.照着书敲了一段程序.结果是"v' : undeclared identifier"说是有个错误.我不大懂.大哥帮帮忙吧.谢谢
程序:
#include<iostream>
using namespace std;
class ARR
{
    int m;
    int a[100];
public:
    ARR(int x[],int size)
    {
        m=size;
        for(int i=0;i<m;i++)
            a[i]=x[i];
    }
    void delsame();
    void show()
    {
        for(i=0;i<m;i++)
        {
            cout<<a[i]<<"\t";
            if((i+1)%5==0)
                cout<<edl;
        }
        cout<<endl;
    }
};
void ARR::delsame()
{
    int i,j;
    for(i=0;i<m-1;i++)
    {
        if(a[i]=a[i+1])
        {
            for(j=i+1;j<m-1;j++)
        {
            a[j]=a[j+1];
        }
        m--;
        i--;
        }
    }
}
int main()
{
    int b[16]={1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10}
    ARR v(b,sizeof(b)/sizeof(b[0]));
    v.show();
    v.delsame();
    v.show();
    return 0;
}
12 回复
#2
sunkaidong2008-03-23 19:43
好了有点小错
#include<iostream>
using namespace std;
class ARR
{
    int m;
    int a[100];
public:
    ARR(int x[],int size)
    {
        m=size;
        for(int i=0;i<m;i++)
            a[i]=x[i];
    }
    void delsame();
    void show()
    {
        for(int i=0;i<m;i++)
        {
            cout<<a[i]<<"\t";
            if((i+1)%5==0)
                cout<<endl;
        }
        cout<<endl;
    }
};
void ARR::delsame()
{
    int i,j;
    for(i=0;i<m-1;i++)
    {
        if(a[i]=a[i+1])
        {
            for(j=i+1;j<m-1;j++)
        {
            a[j]=a[j+1];
        }
        m--;
        i--;
        }
    }
}
int main()
{
    int b[20]={1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10};
    ARR v(b,sizeof(b)/sizeof(b[0]));
    v.show();
    v.delsame();
    v.show();
    return 0;
}
#3
flyue2008-03-23 19:44
你改为这样就没问题了:
#include <iostream>
using namespace std;
class ARR
{
    int m;
    int a[100];
public:
    ARR(int x[], int size)
    {
        m = size;
        for(int i = 0; i < m; i++)
            a[i] = x[i];
    }
    void delsame();
    void show()
    {
        for(int i = 0; i < m; i++)
        {
            cout << a[i] << "\t";
            if((i + 1) % 5 == 0)
                cout << endl;
        }
        cout<<endl;
    }
};
void ARR::delsame()
{
    int i, j;
    for(i = 0; i < m - 1; i++)
    {
        if(a[i] = a[i + 1])
        {
            for(j = i + 1; j < m - 1;j++)
            {
                a[j] = a[j + 1];
            }
            m--;
            i--;
        }
    }
}
int main()
{
    int b[] = {1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10};
    ARR v(b, sizeof(b) / sizeof(b[0]));
    v.show();
    v.delsame();
    v.show();
    return 0;
}
#4
aipb20072008-03-23 20:00
提个意见,大家贴程序求助或者帮求助者修改程序,特别是整段整段的,最好注释一下,你改动了什么地方,别人的错的什么地方。

谢谢合作,o(∩_∩)o...
#5
bestcln2008-03-23 20:06
呵呵.知道了.太大意了
#6
bestcln2008-03-23 20:08
恩.同意.我对着程序查了半天才,找到的.不过,还是谢谢楼上的两位大哥了
#7
sunkaidong2008-03-23 20:20
谢谢斑竹的建议。。。不过我这个好像又没注意。。下次改。。。呵呵
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template <class T>
class ARR
{
    int m;
    vector<T> vec ;
    vector<T>::iterator it;
public:
    ARR(T a[],int size)
    {
     vector<T> vec1(a,a+size);
     vec=vec1;
    }
    void delsame()
    {
        sort(vec.begin(),vec.end());
        it=unique(vec.begin(),vec.end());
        vec.erase(it,vec.end());
    }
    void show()
    {   
        for(vector<int>::iterator iter=vec.begin(); iter!=vec.end();iter++)
        {
            cout<<*iter<<"\t";            
        }
        cout<<endl;
    }
};

int main()
{
    int b[]={1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10};
    ARR<int> v(b,sizeof(b)/sizeof(b[0]));
    v.show();
    v.delsame();
    v.show();
    return 0;
}
#8
bestcln2008-03-23 20:36
不好意思.这个程序的目地是删除序列中的想同的数.但是结果没有把删除的结果显示出来啊
#9
sunkaidong2008-03-23 20:39
我的是把删除过后的结果显示出来。。结果是1。。。10
#10
bestcln2008-03-23 20:42
非常感谢
#11
gehouse2008-03-25 17:28
#include<iostream>
using namespace std;
class ARR
{
    int m;
    int a[100];
public:
    ARR(int x[],int size)
    {
        m=size;
        for(int i=0;i<m;i++)
            a[i]=x[i];
    }
    void delsame();
    void show()
    {
        for(int i=0;i<m;i++)//I没有定义,加int定义
        {
            cout<<a[i]<<"\t";
            if((i+1)%5==0)
                cout<<endl;//不是edl,应该是endl
        }
        cout<<endl;
    }
};
void ARR::delsame()
{
    int i,j;
    for(i=0;i<m-1;i++)
    {
        if(a[i]=a[i+1])
        {
            for(j=i+1;j<m-1;j++)
        {
            a[j]=a[j+1];
        }
        m--;
        i--;
        }
    }
}
int main()
{
    int b[17]={1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10};//初始化数组溢出,由16改成17
    ARR v(b,sizeof(b)/sizeof(b[0]));
    v.show();
    v.delsame();
    v.show();
    return 0;
}
#12
elegant872008-03-25 20:59
低级错误
以后要小心一点啊
#13
haxin2008-03-26 09:55
帮顶,学习ing
1