bachelorjin 2016-05-04 09:40 采纳率: 0%
浏览 1498
已结题

在类中使用string类的问题。我定义的构造函数,想把字符串赋给string对象,却实现不了。

//12.2.ano.h
#ifndef STRING2_H_
#define STRING2_H_

#include
#include

using std::string;
using std::cout;
using std::cin;
using std::endl;

class String
{
private :
string str;
public :
String();
String(const string cs);
String(const String & s);
~String();
String & operator=(const String & s);
String & operator=(const string cs);
friend std::ostream & operator<<(std::ostream & os, String & s);
};

#endif

//string.cpp 实现文件
#include "12.2.h"
#include

String::String()
{

}

String::String(const string cs)
{
str = cs;
}

String::String(const String & s)
{
str = s.str;
}

String::~String()
{

}

String & String::operator=(const string cs)
{
str = cs;
return *this;
}

String & String::operator=(const String & s)
{
str = s.str;
return *this;
}

std::ostream & operator<<(std::ostream & os, String & s)
{
os << s.str;
return os;
}

//main.cpp 主函数
#include
#include "12.2.ano.h"
int main()
{
String a1 = "I know";
String a2 = (string)"I know";
cout << a1;
return 0;
}

  • 写回答

1条回答

  • dajiaxuejavascript 2016-05-05 01:59
    关注

    #include
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    class String
    {
    private :
    string str;
    public :
    String();
    String(const string cs);
    String(const String & s);
    ~String();

    评论

报告相同问题?

悬赏问题

  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题