2013-02-27 57 views
0

我正在处理以下问题。是正式的,我使用VS2010 Ultimate和我尝试写一个Windows窗体应用程序,但我得到指定的错误:C++/CLI - 错误C2664再次

1>f:\baza danych\baza\baza\Form5.h(475): error C2664: 'Bazadanych::Dodaj1' : cannot   convert parameter 1 from 'Car' to 'Car' 
1>   Cannot copy construct class 'Car' due to ambiguous copy constructors or no available copy constructor 

,这里是Car.h在那里我有这个类的声明

public ref class Car 
{ 
public: 
    String^ category; 
    String^ model; 
    String^ rocznik; 
    String^ cena; 

    Car(){}; 
    Car(String^ ,String^ ,String^); 
    void edytuj(String^ ,String^ ,String^); 
    String^ getmodel(){return this->model;}; 
    String^ getrocznik(){return this->rocznik;}; 
    String^ getcena(){return this->cena;}; 
    virtual String^ getcat() 
    { 
     this->category="To rent"; 
     return this->category; 
    };` 
} 

定义:

Car::Car(String^ model1,String^ rocznik1,String^ cena1) 
    { 
     this->model=model1; 
     this->rocznik=rocznik1; 
     this->cena=cena1; 
    }; 

    void Car::edytuj(String^ model1,String^ rocznik1,String^ cena1) 
    { 
     this->model=model1; 
     this->rocznik=rocznik1; 
     this->cena=cena1; 
    }; 

类的宣言其中错误提到的方法是:

public ref class Bazadanych 
{ 
public: 
cliext::list<Car^> Bazatorent; 
cliext::list<Rented^> Bazarented; 
cliext::list<Unavaible^> Bazaunavaible; 
cliext::list<Car^>::iterator it1; 
cliext::list<Rented^>::iterator it2; 
cliext::list<Unavaible^>::iterator it3; 

Bazadanych() 
{ 
    it1=Bazatorent.begin(); 
    it2=Bazarented.begin(); 
    it3=Bazaunavaible.begin(); 
}; 
bool Empty(); 
void Dodaj1(Car); 
void Dodaj2(Rented); 
void Dodaj3(Unavaible); 
void Usun1(Car); 
void Usun2(Rented); 
void Usun3(Unavaible); 
void Czysc(); 
}; 

和定义:

void Bazadanych::Dodaj1(Car Element) 
{ 
this->Bazatorent.push_back(Element); 
}; 

我在separatly的.h和.cpp文件定义和声明。对于其他方法“Dodaj”和“Usun”,我有完全相同的问题。如果它可以帮助Car类成为Rented和Unavaible类的基类。我在C++/CLI中很新,所以如果有人能帮助我,我将非常感激。

回答

1

我发现错误信息奇怪,因为它是一个托管类。但是,您可以通过将方法的签名更改为:

void Bazadanych::Dodaj1(Car^ Element) // notice the "^" 

与其他类似的方法相同。我猜如果没有帽子(^),编译器会将该变量视为一个普通的C++类,因此需要一个拷贝构造函数,尽管托管类甚至没有拷贝构造函数(你可以写他们,但他们从来没有像常规的C++类那样隐式调用)。

编辑:关于您的评论的错误:不是实例化类是这样的:

Car car; 

做这样的:

Car^ car = gcnew Car(); 
+0

加入后^我得到改变错误” C2664'˚F :\ baza danych \ baza \ baza \ Form5.h(485):error C2664:'Bazadanych :: Dodaj3':无法将参数1从'Unavaible'转换为'Unavaible ^' 1>没有用户定义的转换运算符可用,或者 1>没有可以执行th的用户定义转换操作符是转换,或者操作员不能被称为' – user2116987 2013-02-27 21:00:00

+0

感谢它正在工作;) – user2116987 2013-02-27 21:13:38

0

它说明了这是什么意思:你没有Car的拷贝构造函数。这可能是这样的:

Car::Car(const Car& c) { 
    /* your code here*/ 
}; 

一些背景herehere