我有一個重載的巨大問題[]
我完全按照示例中所示使用它,它不起作用甚至沒有被編譯器看到。重載[]運算符和複製構造函數不起作用
我得到的錯誤:
no match for 'operator<<' in 'std::cout << * moj
的第二個問題是,即使我使用拷貝構造,如果我刪除原來的對象複製一個自敗。但是現在當我添加析構函數時就崩潰了。
C:\Documents and Settings\Duke\Moje dokumenty\MaciekK\String\main.cpp|90|error: no match for 'operator<<' in 'std::cout << * moj'|
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstring>
using namespace std;
class String{
public:
char* napis;
int dlugosc;
char & operator[](int el) {return napis[el];}
const char & operator[](int el) const {return napis[el];}
String(char* napis){
this->napis = napis;
this->dlugosc = this->length();
}
String(const String& obiekt){
int wrt = obiekt.dlugosc*sizeof(char);
//cout<<"before memcpy"<<endl;
memcpy(this->napis,obiekt.napis,wrt);
//cout<<"after memcpy"<<endl;
this->dlugosc = wrt/sizeof(char);
}
~String(){
delete[] this->napis;
}
int length(){
int i = 0;
while(napis[i] != '\0'){
i++;
}
return i;
}
void show(){
cout<<napis<<" dlugosc = "<<dlugosc<<endl;
}
};
int main()
{
String* moj = new String("Ala ma kota");
// cout<< moj[0] <<endl; // I GETT ERROR NO MATH FOR OPERATO<< IN STD:: COUTN<< * MOJ
String* moj2 = new String(*moj);
moj->show();
delete moj;
moj2->show();
return 0;
}
請你可以編輯你的問題,包括**確切的**錯誤信息。 –
這就是我可以粘貼的; /。 – Yoda