Incompatible function declaration

只愿长相守 提交于 2019-12-11 04:18:30

问题


I'm stuck, why am i getting an error: declaration is incompatible...

'void A::function(int,int,std::vector<_Ty> *)' : overloaded member function not found in 'A' error C2061: syntax error : identifier 'vector' 1> with 1> [ 1> _Ty=Point 1> ]

//cpp file
void A::function(int a, int b, vector<B> *p)
{    

}

//header file

class B
{
public:
     int q;
};
class A
{     
public:    
     void function(int a, int b, vector<B> *p);    
};

回答1:


It's because the header of the function should be exactly the same.

//cpp file 
void A::function(int a, int b, vector<B>* c) { }  

//header file  
Class B {
public:
    int q; 
}; 
class A {
public:
    void function(int a, int b, vector<B> *line);     
}; 

or :

//cpp file 
void A::function(int a, int b, vector<B>& c) { }  

//header file  
Class B {
public:
    int q;
};
class A {
public:
    void function(int a, int b, vector<B> &line);     
}; 

However, when calling the function in the first case, you should replace the * with & if passing an object, so the local pointer will get the address of the passed object. Or manually pass a pointer.




回答2:


Problems!!!!

void function(int a, int b, vector<B> *line); and void function(int a, int b, vector<B> & line); are two different signatures (function prototypes).

More importantly there is no such keyword Class in C++.




回答3:


Well, for starters, you're missing a semicolon at the end of B. Additionally, you're using Class instead of class.

For the signature itself, your declaration (in the header file) takes a pointer to a vector while your definition (in the .cpp file) takes a reference.

//cpp file
void A::function(int a, int b, vector<B>& c) // // Arguments are an int, an int, and a vector<B> reference.
{    

}

//header file

class B
{
public:
     int q;
};

class A
{     
public:    
     void function(int a, int b, vector<B>& line);
        // Same arguments.
};


来源:https://stackoverflow.com/questions/5339395/incompatible-function-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!