2013-02-27 56 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