class A{
private:
int a,b,c;
public:
A(int a,int b,int c):a(a),b(b),c(c){};
A(const A& temp){
a = temp.a;
b = temp.b;
c = temp.c;
}
void print(){
cout<<a<<" ";
cout<<b<<" ";
cout<<c<<endl;
}
};
int main(){
A a(1,2,3);
A b(a);
b.print();
}
因为A(const A& temp)构造函数是与temp属于同一个类,因此可以在类内直接调用temp的私有变量,以下情况属于类外调用,则不可,如下:
class B{
private:
int e,f,g;
public:
//B(int a,int b,int c):a(a),b(b),c(c){};
B(const A& temp){
e = temp.a;
f = temp.b;
g = temp.c;
cout<<temp.a<<" ";
cout<<temp.b<<" ";
cout<<temp.c<<" ";
}
void print(){
cout<<e<<" ";
cout<<f<<" ";
cout<<g<<endl;
}
};
int main(){
A a(1,2,3);
B b(a);
b.print();
cout<<a.a<<endl;//类外调用,不可以
}
因为B与temp是不同的类,属于类外调用,不可以
另外直接在main函数这样的外部函数调用也是不可以的
出现这样的问题一般是在const成员函数中调用了非const成员函数
E:\C++Project\MyTinySTLNew\Test\ListTest.cpp(85): 参见对正在编译的函数 模板 实例化“void TinySTL::list<int,TinySTL::allocator>::insert(TinySTL::list_iterator,InputIterator,InputIterator)”的引用
with
[
T=int
, InputIterator=int
]
经常出现这个,因为在于重载的insert函数中有参数为(iterator position,const size_type& n,const value_type& x)和(iterator position,InputIterator first,InputIterator last)的,当在调用insert(position,10,2)的时候int型无法转换成size_t,进而去调用了后者,造成了参数错误,所以要显示转型为size_t,不然会出错!
- *error: cannot convert ‘__gnu_cxx::__normal_iterator<int*, std::vector >’ to ‘const void*’
memcpy(result, first, (last - first) * sizeof(first));
*这个问题是因为,经过traits萃取出来的类型变成了vector::iterator,而memcpy无法将其转换成const void,所以把函数改成了
memcpy(result, &(*first), (&(*last) - &(first)) * sizeof(first));
return result + (&(last) - &(first));
通过再取&得到T类型才可以使用memcpy!