2009-11-11 54 views
0

我想创建一个自定义类型的构造函数,但由于某种原因,它试图打电话,我在猜测是另一个类的构造函数定义中的构造函数。无法找到任何符合我在其他任何问题中遇到的相同症状的东西,因为我可能不知道我在找什么。构造函数与自定义类作为参数,抛出'没有匹配的函数调用...'

当我打电话:

LatLngBounds clusterBounds(&boundsNorthEast, &boundsSouthWest); 
在main.cpp中

,在LatLngBounds.cpp我得到 “没有找到调用“经纬度匹配功能可按:经纬度()” 就行抛出两次:

LatLngBounds::LatLngBounds(LatLng &newSouthWest, LatLng &newNorthEast) 

任何人有任何想法?

德鲁J.索内。

IDE:Xcode的3.2(靶向调试10.5)
OS:OSX 10.6
编译器:GCC 4.2
拱:x86_64的



main.cpp中:

std::vector<std::string> argVector; 

... fill up my argVector with strings.. 

vector<double> boundsVector = explodeStringToDouble(argVector[i]); 
LatLng boundsNorthEast(0, boundsVector[0], boundsVector[1]); 
LatLng boundsSouthWest(0, boundsVector[2], boundsVector[3]); 
LatLngBounds clusterBounds(&boundsNorthEast, &boundsSouthWest); 

LatLngBounds.h

#ifndef __LATLNGBOUNDS 
#define __LATLNGBOUNDS 
#include "LatLng.h" 
class LatLngBounds { 
private: 
    LatLng northEast; 
    LatLng southWest; 
public: 
    LatLngBounds(LatLng&,LatLng&); 
}; 
#endif 

LatLngBounds.cpp

#include "LatLngBounds.h" 
#include "LatLng.h" 

LatLngBounds::LatLngBounds(LatLng &newSouthWest, LatLng &newNorthEast) 
{ 
    this->southWest = newSouthWest; 
    this->northEast = newNorthEast; 
}; 

LatLng.h

#ifndef __LATLNGDEF 
#define __LATLNGDEF 
class LatLng { 
public: 
    LatLng(int,double,double); 
private: 
    double lat, lng; 
    int id; 
}; 
#endif 

LatLng.cpp

#include "LatLng.h" 
LatLng::LatLng(int newId, double newLat, double newLng) 
{ 
    /* Grab our arguments */ 
    id = newId; 
    lat = newLat; 
    lng = newLng; 
}; 

回答

3

在你的类,你有经纬度对象的两个实例。为了构建你的对象,编译器也需要构造它们。

class LatLngBounds { 
private: 
    LatLng northEast; 
    LatLng southWest; 

由于类经纬度没有默认构造函数,你需要明确地告诉编译器如何构建这些对象:

LatLngBounds::LatLongBounds(..constructor args..) 
    : northEast(..args for northEast constructor call ..), 
     southWest(..args for southWest constructor call ..) 
{ 
} 
+0

太棒了。这也是函数调用中的&,但这是我无法找到的。谢谢。 – Drew 2009-11-11 07:25:16

3

LatLngBounds::LatLngBounds(LatLng &newSouthWest, LatLng &newNorthEast) 

预计LatLng类型的参数,通过引用implictly通过。

调用LatLngBounds clusterBounds(&boundsNorthEast, &boundsSouthWest);改为传递两个指针(类型LatLng*)。

尝试,而不是:

LatLngBounds clusterBounds(boundsNorthEast, boundsSouthWest); 
+0

而且,我可以补充说,通过const引用采取参数是一个好主意。 – rlbond 2009-11-11 06:58:39

+0

同意,const LatLng&会更好。 – s1n 2009-11-11 06:59:42

0

你确定你引用的错误消息是否已经完成?它表明,在你粘贴的时候,LatLng没有默认的空构造函数,编译器会免费给你。

然而,看起来这行main.cpp中是错误的:

LatLngBounds clusterBounds(&boundsNorthEast, &boundsSouthWest); 

的构造方法的LatLngBounds的定义是2到经纬度对象非const引用,但你是路过2个指针经纬度对象。只需通过他们没有指针:

LatLngBounds clusterBounds(boundsNorthEast, boundsSouthWest); 
+0

如果有其他用户定义的构造函数,编译器将不会创建默认构造函数。 – UncleBens 2009-11-11 07:58:36

相关问题