#include <iostream>
#include <vector>
using namespace std;class Test
{
public:
Test(int a)

m_a = a; 
m_str = NULL;
m_str = new char[4]; 
if (m_str == NULL)
cout<<"invalid memory!"<<endl;
memcpy(m_str, "abc", 3);
cout<<"Constructor"<<endl;
} Test(Test &test);
{
this->m_a = test.m_a;
this->m_str = new char[4];
strcpy(this->m_str, test.m_str);
}

void operator=(Test &test);
{
this->m_a = test.m_a;
this->m_str = new char[4];
strcpy(this->m_str, test.m_str);
} ~Test()
{
int a = m_a;
if (m_str != NULL)
{
delete m_str;
m_str = NULL;
}
cout<<"Destructor"<<endl;
}
private:
int m_a;
char *m_str;
};int main()
{
Test t1(1), t2(2);
Test t3(3);
vector<Test> vt;
vt.push_back(t1);
vt.push_back(t2);
vt.push_back(t3);
vt.clear();
return 0;
}为什么提示:
1.binary '=' : no operator defined which takes a right-hand。
2.class 'Test' : no copy constructor available高手指教

解决方案 »

  1.   

    看注解#include <iostream> 
    #include <vector> 
    using namespace std; class Test 

    public: 
    Test(int a) 

    m_a = a; 
    m_str = NULL; 
    m_str = new char[4]; 
    if (m_str == NULL) 
    cout < <"invalid memory!" < <endl; 
    memcpy(m_str, "abc", 3); 
    cout < <"Constructor" < <endl; 
    } //参数应该是const Test& testTest(Test &test); //这里的分号需要去掉

    this->m_a = test.m_a; 
    this->m_str = new char[4]; 
    strcpy(this->m_str, test.m_str); 

    //参数和分号同上
    //函数应该返回Test&
    void operator=(Test &test); 

    this->m_a = test.m_a; 
    this->m_str = new char[4]; 
    strcpy(this->m_str, test.m_str); 
    } ~Test() 

    int a = m_a; 
    if (m_str != NULL) 

    delete m_str; 
    m_str = NULL; 

    cout < <"Destructor" < <endl; 

    private: 
    int m_a; 
    char *m_str; 
    }; int main() 

    Test t1(1), t2(2); 
    Test t3(3); 
    vector <Test> vt; 
    vt.push_back(t1); 
    vt.push_back(t2); 
    vt.push_back(t3); 
    vt.clear(); 
    return 0; 

      

  2.   

    Test& operator=(const Test &test); 

    this->m_a = test.m_a; 
    this->m_str = new char[4]; 
    strcpy(this->m_str, test.m_str); 
    return *this;

      

  3.   

    c++里面的复制构造和对象赋值都有标准的写法
    Test(const Test &test); 
    {
    //............
    }Test& operator=( const Test& rhs)//这里好像可以返回void,不过一般都是返回引用,单词好像不对.
    {............}