c ++将数组声明为函数

时间:2013-10-23 07:57:06

标签: c++ arrays function parallel-arrays

我面临阵列的一些问题,因为我有.h和.cpp文件,所以我按照我们通常声明函数的规范来声明它们吗?

pointtwod.h

class PointTwoD
{
    private:
        int xcord,ycord;
        float civIndex;
        LocationData locationdata;

    public:
            PointTwoD();

            PointTwoD(int, int, string, int, int, float, float, float);

        //set/mutator function
        void setxcord(int);
        void setycord(int);

        //get/accessor function
        int getxcord();
        int getycord();

        void storedata(int, int, float);

};

pointtwod.cpp

//declaring array
void PointTwoD::storedata(int xord[], int yord[], float civ[])
{
    int i=0;
    //int size = sizeof(xord)/sizeof(xord[0]);
    int size = 100;
    for(i=0;i<100;i++)
    {
        cout << "key in num for xord: " << endl;
        xord[i] = xcord;
        cout << "key in value for yord: " << endl;
        xord[i] = ycord;
        cout << "key in value for civ: " << endl;
        civ[i] = locationdata.getCivIndex();
    };
}
int main()
{
    PointTwoD pointtwod;
    pointtwod.storedata(xord[], yord[], civ[]);
}

当我编译错误消息时,我得到的甚至当我放入int xord [];在我的PointTwoD.h文件中:

PointTwoDImp.cpp:99:6: error: prototype for 'void PointTwoD::storedata(int*,int*,float*) does not match any in class 'PointTwoD'

PointTwoD.h:48:8: error: candidate is: void PointTwoD::storedata(int, int, float)

PointTwoDImp.cpp: 135:22: error: 'xord' was not declared in this scope
PointTwoDImp.cpp: 135:27: expected primary-expression before ']' token
PointTwoDImp.cpp: 135:30: error: 'yord' was not declared in this scope
PointTwoDImp.cpp: 135:35: expected primary-expression before ']' token
PointTwoDImp.cpp: 135:38: error: 'civ' was not declared in this scope
PointTwoDImp.cpp: 135:42: expected primary-expression before ']' token

1 个答案:

答案 0 :(得分:0)

PointTwoDImp.cpp:99:6: error: prototype for 'void PointTwoD::storedata(int*,int*,float*) does not match any in class 'PointTwoD'

这意味着该函数与您在标题中声明的原型不匹配,即:

void storedata(int, int, float);

声明应该是:

void storedata(int*, int*, float*);

或者:

void storedata(int xord[], int yord[], float civ[]);

其余的错误是因为你没有在main中声明xord[], yord[], or civ[],而是将它们传递给函数。

因此,在将它们传递给函数之前,需要在main中声明三个数组。