【问题标题】:No matching function call Strange error没有匹配的函数调用奇怪的错误
【发布时间】:2015-11-29 10:16:11
【问题描述】:
    #include<iostream>
    using namespace std;
    const int size=4;

    template<class datatype>
    class vector
     {

        datatype *v;

        public:
            vector()
            {

                v=new datatype[size];
                for(int i=0;i<size;i++)
                    {
                        v[i]=0;//initilaizing vector
                    }
            }
            vector(datatype *ptr)
            {
                //v=ptr;
                for(int i=0;i<size;i++)
                {
                    v[i]=ptr[i];
                }
            }

            datatype operator*(vector &obj)
            {
                datatype sum=0;
                for(int i=0;i<size;i++)
                    {
                        sum+=(this)->v[i] * obj.v[i];

                    }
                    return sum;
            }
};

     int main()
    {
        int x[4]={1,2,9,11};
        int y[4]={4,7,7,4};

        vector<int> v1;
        v1(x);//no matching call
        vector<int> v2;
        v2(y);//no matching call
       // v1(x);
       // v2(y);
         int total=v1*v2;
         cout<<"Total of v1*v2="<<total<<endl;
    }    

我在其中传递整数数组 x 和 y 的构造函数给了我以下错误。
错误:对“(向量)(int [4])”的调用不匹配。
错误:对 (vector) (int [4]) 的调用不匹配。将数组分配给指针时存在一些问题。
谁能更正我的代码。

【问题讨论】:

    标签: c++ template-classes


    【解决方案1】:

    在您的代码中:

        vector<int> v1;
        v1(x);//no matching call
        ^^^^^^
    

    是一个函数调用,但您没有名称为 v1 的函数,而且您的向量也没有重载 operator()。你可能想要:

        vector<int> v1(x);
    

    另外,你需要:

    v=new datatype[size];
    

    vector(datatype *ptr) 构造函数的开头。

    【讨论】:

    • 我试过vector v1(x);这没有给我这样的错误,但控制台窗口关闭。
    • @user5501265 查看我的编辑,您的代码还有很多其他问题,但我知道您正在学习 C++。
    • 感谢 Marcin 对我的理解。我添加了这个 vect(datatype *ptr):vect() 。我将我的班级名称更改为 vect 并且它现在可以工作了。
    猜你喜欢
    • 2012-12-28
    • 2015-03-31
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多