如何在类中使用字符串和/或char数组... [英] How do i.use string and/or char arrays in classes...

查看:101
本文介绍了如何在类中使用字符串和/或char数组...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个非常简单的c ++程序,用于使用类打印发件人的姓名,接收者和电子邮件的时间。它显示以下错误。我在类中使用字符串和/或char数组时遇到了困难。但是,如果我使用单个字符,则不会显示错误。请帮帮我。





我的尝试:



 #include< stdio.h> 
#include< conio.h>
#include< iostream.h>
#include< stdlib.h>
类消息
{
private:
char sender [10];
char收件人[10];
int time;
public:
message(char s [10],char r [10],int t);
void print();
};
message :: message(char s [10],char r [10],int t)
{
sender = s; // ERROR lvalue required
recipient = r; // ERROR左值需要
time = t;
} //永远不会使用ERROR参数s和r
void message :: print()
{
cout<<发件人是:<< sender ;
cout<<收件人是:<< recipient;
cout<<当前时间是:<< time;
}
main()
{
char s [10],r [10];
int t;
clrscr();
cout<<输入发件人姓名:;
cin>> s;
cout<<输入收件人姓名:;
cin>> r;
cout<<输入当前时间:;
cin>> t;
消息c(s,r,t);
c.print();
getch();
}

解决方案

你没有使用字符串你是使用 char 数组。在这种情况下,您不能使用像 sender = s 这样的表达式从一个表达式复制到另一个表达式。你需要使用 strcpy 或类似的函数。


嘿,你正在使用 C ++ ,不是吗?

std :: string 在这种情况下很方便:

< pre lang =C ++> #include < < span class =code-leadattribute> string >
#include < iostream >
使用 命名空间 std ;

class message
{
string sender;
字符串收件人;
int 时间;
public
message(string s,string r, int t):发件人,收件人(r),时间(t){}
void print();
};

void message :: print()
{
cout<< 发件人是<<发件人<< ENDL;
cout<< consnt是<<收件人<< ENDL;
cout<< 时间是<<时间<< ENDL;

}

int main()
{
string s,r;
int t;
cout<< 输入发件人名称:;
cin>> s;
cout<< 输入收件人姓名:;
cin>> r;
cout<< 输入时间:;
cin>> t;
消息c(s,r,t);
c.print();
}


I am writing a very simple c++ program to print the name of the sender, recepient and time of an  email message using classes. It shows the following errors. I am having difficulty using string and/or char arrays in classes. However, if I use single char, it does not show error. Please help me out.



What I have tried:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class message
{
private:
char sender[10];
char recipient[10];
int time;
public:
message (char s[10],char r[10],int t);
void print();
};
message::message (char s[10],char r[10],int t) 
{
sender=s;//  ERROR lvalue required
recipient=r;    // ERROR lvalue required
time=t;
} // ERROR parameter s and r are never used
void message::print()
{
cout<<"the sender is:  "<<sender;
cout<<"the recipient is : "<<recipient;
cout<<"current time is : "<<time;
}
main()
{
char s[10], r[10];
int t;
clrscr();
cout<<"enter senders name :";
cin>>s;
cout<<"enter recipient name :";
cin>>r;
cout<<"enter current time :";
cin>>t;
message c(s,r,t);
c.print();
getch();
}

解决方案

You are not using strings you are using char arrays. In which case you cannot use expressions like sender=s to copy from one to the other. You need to use strcpy or similar function.


Hey, you are using C++, aren't you?
std::string is handy, in such a scenario:

#include <string>
#include <iostream>
using namespace std;

class message
{
  string sender;
  string recipient;
  int time;
public:
  message(string s, string r, int t): sender(s), recipient(r), time(t){}
  void print();
};

void message::print()
{
  cout << "the sender is " << sender << endl;
  cout << "the recipent is " <<  recipient << endl;
  cout << "the time is " << time << endl;

}

int main()
{ 
  string s,r;
  int t;
  cout<<"enter senders name :";
  cin>>s;
  cout<<"enter recipient name :";
  cin>>r;
  cout<<"enter time :";
  cin>>t; 
  message c(s,r,t);
  c.print();
}


这篇关于如何在类中使用字符串和/或char数组...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆