返回类型以返回所有字符串 [英] Return type to return all string

查看:105
本文介绍了返回类型以返回所有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,它返回成员函数,但只返回emp_name的最后一个字符。

非常感谢任何帮助。



我尝试过:



I have this code and it returns the member function as written but return only the last character of emp_name.
any assistance is highly appreciated.

What I have tried:

#include <iostream>

using namespace std;

class emp {
public:

	int getInfo(char emp_name, int emp_id_number, int emp_age) {
		cout << "Employee Name: " << emp_name << "\n";
		cout << "Employee Id Number: " << emp_id_number << "\n";
		cout << "Employee Age: " << emp_age;
	}
};
int main() {
	emp emp1;
	emp1.getInfo('Felix', 635932, 23);
}

推荐答案

int getInfo(char emp_name, int emp_id_number, int emp_age) {



您已将 emp_name 参数声明为单个字符,而不是字符串。它应该是:


You have declared the emp_name parameter as a single character, rather than a character string. It should be:

int getInfo(char* emp_name, int emp_id_number, int emp_age) { // char*


您可能希望将字符串a传递给 getInfo()但实际上你传递的是一个角色。



改变这两行:

You probably want to pass a string a to getInfo() but you are actually passing a single character.

Change these two lines:
// Must be char* here
int getInfo(char *emp_name, int emp_id_number, int emp_age)

// Must pass a string here using double quotes
emp1.getInfo("Felix", 635932, 23);





您已定义 getInfo()返回 int 但你没有 return 语句。



我建议在编译时使用高警告级别(-Wall with G ++,/ W4在项目设置中使用VS)。那么你的代码至少会有两个警告。



You have defined getInfo() to return an int but you have no return statement.

I suggest to use a high warning level when compiling (-Wall with G++, /W4 in the project settings with VS). Then you would have got at least two warnings for your code.


#include <iostream>

using namespace std;

class emp {
public:

	int getInfo(char* emp_name, int emp_id_number, int emp_age) {
		cout << "Employee Name: " << emp_name << "\n";
		cout << "Employee Id Number: " << emp_id_number << "\n";
		cout << "Employee Age: " << emp_age;
	}
};
int main() {
	emp emp1;
	emp1.getInfo("Felix", 635932, 23);
}


这篇关于返回类型以返回所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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