strcpy_s不接受两个参数 String的实现
2014-05-05 23:40
585 查看
#include <iostream>
#include <cstring>
using namespace std;
class String
{
public:
//String(); //default constructor
String(const char * str =NULL); //common constructor
String(const String& another); //copy constructor
~String(); //destructor
String& operator=(const String& another);
friend ostream & operator <<(ostream& output, String& str );
friend istream & operator >>(istream& input , String& str);
private:
char * m_data;
};
//default constructor
//when to use it:
//String a;
//owner don't write it, while write constrcutor with parameter,
//Q: m_data is NULL;
//or m_data=new char[0]; m_data='\0';
//A:
/*
String::String()
{
cout << "Call 'Default Constructor'" << endl;
m_data = NULL;
}
*/
//common constructor
//String a("Hello World!");
//Q: whether judge str is null
//if str == NULL; then m_data is null or '\0'
//A:
String::String(const char* str)
{
cout << "Call 'Common Constructor'" << endl;
m_data = new char[strlen(str)+1];
strcpy_s(m_data,str);
}
//copy constructor
// String a(b); //b is an object of String
String::String(const String& another)
{
cout << "Call 'Copy Constructor'" << endl;
m_data = new char[strlen(another.m_data)+1];
strcpy_s(m_data,another.m_data);
}
//destructor
//in the final scope of the object
String::~String()
{
cout << "Call 'Destructor'" << endl;
delete[]m_data;
// or just delete m_data; //as char* is a data type
}
String& String::operator=(const String& another)
{
cout << "Call 'Overload ='" << endl;
//1 judge this == another
//by address
if (this == &another )
{
return *this;
}
//2 free prevoius memory
delete []m_data;
//3 allocating new memory
this->m_data = new char[strlen(another.m_data)+1];
strcpy_s(this->m_data,another.m_data);
//4 return this
return *this;
}
ostream& operator<<(ostream &output, String &str)
{
cout << "Call 'Overload <<'" << endl;
output << str.m_data;
return output;
}
istream& operator>>(istream &input, String &str)
{
cout << "Call 'Overload >>'" << endl;
input >> str.m_data;
return input;
}
int main()
{
String a("Hello ");
String b("World!");
cout << a << b << endl;
cin >> a >> b;
cout << a << b << endl;
system("pause");
return 0;
}
在VS2013上还没build成功,因为strcpy_s不接受两个参数, 亲们帮忙看看是什么原因?
解决方法之:add the second parameter in strcpy_s
int length=strlen(anoter.m_data);
用strcpy_s(m_data,length+1,another.m_data)替换strcpy_s(m_data,another.m_data); build succeed
但是 触发新的断点 太晚了 稍后更新
#include <cstring>
using namespace std;
class String
{
public:
//String(); //default constructor
String(const char * str =NULL); //common constructor
String(const String& another); //copy constructor
~String(); //destructor
String& operator=(const String& another);
friend ostream & operator <<(ostream& output, String& str );
friend istream & operator >>(istream& input , String& str);
private:
char * m_data;
};
//default constructor
//when to use it:
//String a;
//owner don't write it, while write constrcutor with parameter,
//Q: m_data is NULL;
//or m_data=new char[0]; m_data='\0';
//A:
/*
String::String()
{
cout << "Call 'Default Constructor'" << endl;
m_data = NULL;
}
*/
//common constructor
//String a("Hello World!");
//Q: whether judge str is null
//if str == NULL; then m_data is null or '\0'
//A:
String::String(const char* str)
{
cout << "Call 'Common Constructor'" << endl;
m_data = new char[strlen(str)+1];
strcpy_s(m_data,str);
}
//copy constructor
// String a(b); //b is an object of String
String::String(const String& another)
{
cout << "Call 'Copy Constructor'" << endl;
m_data = new char[strlen(another.m_data)+1];
strcpy_s(m_data,another.m_data);
}
//destructor
//in the final scope of the object
String::~String()
{
cout << "Call 'Destructor'" << endl;
delete[]m_data;
// or just delete m_data; //as char* is a data type
}
String& String::operator=(const String& another)
{
cout << "Call 'Overload ='" << endl;
//1 judge this == another
//by address
if (this == &another )
{
return *this;
}
//2 free prevoius memory
delete []m_data;
//3 allocating new memory
this->m_data = new char[strlen(another.m_data)+1];
strcpy_s(this->m_data,another.m_data);
//4 return this
return *this;
}
ostream& operator<<(ostream &output, String &str)
{
cout << "Call 'Overload <<'" << endl;
output << str.m_data;
return output;
}
istream& operator>>(istream &input, String &str)
{
cout << "Call 'Overload >>'" << endl;
input >> str.m_data;
return input;
}
int main()
{
String a("Hello ");
String b("World!");
cout << a << b << endl;
cin >> a >> b;
cout << a << b << endl;
system("pause");
return 0;
}
在VS2013上还没build成功,因为strcpy_s不接受两个参数, 亲们帮忙看看是什么原因?
解决方法之:add the second parameter in strcpy_s
int length=strlen(anoter.m_data);
用strcpy_s(m_data,length+1,another.m_data)替换strcpy_s(m_data,another.m_data); build succeed
但是 触发新的断点 太晚了 稍后更新
相关文章推荐
- 用C语言编写函数实现一个整数计算机,程序接受三个参数,第一个参数为操作项,后两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 用C语言编写函数实现一个整数计算机,程序接受三个参数,第一个参数为操作项,后两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- strcpy_s不接受两个参数的问题
- 实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。 例如:输入t
- shell练习:函数实现接受参数,查询用户输入的用户情况(带颜色显示)
- 编写一个排序函数,实现数字排序。排序方法由客户函数实现,函数参数个数为两个,两个参数的关系作为排序后的元素间的关系。
- strcpy 和 memcpy两个函数的实现
- 接受可变参数的函数实现多个数相加
- ASP.NET中实现页面间的参数传递 QueryString/Application/Session/Cookie
- C语言 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
- JAVAWEB开发之Struts2详解(二)——Action接受请求参数、类型转换器、使用Struts2的输入校验、以及遵守约定规则实现Struts2的零配置
- 编写一个函数,有两个参数function(string s1,string s2),找出字符串中s1中s2的个数
- JAVA(Spring mvc)接口实现类 接收参数时出现 For input string: ""问题