English 中文(简体)
超载操作员和制版制造器不工作
原标题:Overloading [] operator and copy constructor doesn t work

我对超载 < > < < i 有很大的问题。 我完全按照实例中显示的那样使用了它,但汇编者甚至看不到它。

我知道错误:

在 std:: cout<<< * moj 中, 运算符 < < 没有匹配

第二个问题是,即使我用复制件构建,如果我删除原始对象, 复制的碎片就会消失。但现在当我添加毁灭器程序时, 崩溃了。

C:Documents and SettingsDukeMoje dokumentyMaciekKStringmain.cpp|90|error: 在 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] !=   ){
        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;
}
问题回答

问题是 >moj String /code>, 不是 String 。 所以 moj[0] 不引用您的 operator< < , 它只是删除了指针 。

你的问题是:

Calling deallocation functions on any address not returned by memory allocation functions is an Undefined Behavior. You have an Undefined Behavior in your code, because You never allocate memory using new [] but you call delete [] in destructor(delete[] this->napis;).

You are not implementing the constructor & the copy constructor correctly.
You need to allocate dynamic memory in constructor and also in the copy constructor. Currently you do not allocate memory in the constructor and in copy constructor you perform a shallow copy instead of an deep copy.

你应该:

String(char* napis)
{
    //I put 20 as size just for demonstration, You should use appropriate size here.
    this->napis = new char[20];   <-------------- This is Important!
    memcpy(this->napis,napis,12);
    this->dlugosc = this->length();
}

String(const String& obiekt)
{

    int wrt = obiekt.dlugosc*sizeof(char);
    this->napis = new char[wrt];  <-------------- This is Important!
    memcpy(this->napis,obiekt.napis,wrt);
    this->dlugosc = wrt/sizeof(char);
}    

另外,您还需要在 moj2 上调用 delete ,以避免程序结束时出现内存泄漏。

delete moj2;

http://ideone.com/LkyOn" rel="no follow" >这里 是您的方案的在线版本,上面提到修改,效果很好。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签