开发者

Incompatible function declaration

开发者 https://www.devze.com 2023-02-18 13:38 出处:网络
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\'

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);    
};


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.


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++.


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.
};
0

精彩评论

暂无评论...
验证码 换一张
取 消