调用“cout”的成员函数 [英] member function calling in "cout"

查看:183
本文介绍了调用“cout”的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个理解类的简单代码,代码工作正常,除非我调用getlastname函数..如果这个函数无关,那么为什么我们编写这个getfirsttname和getlastname函数.. ???

这里是我的代码请有眼睛..

i想要调用获取姓氏功能,但这会产生错误..

i真的需要你的评论在这里..



感谢您的考虑.. !!!!



This is a simple code to understand classes, the code is working good unless i call getlastname function.. if this function have nothing to do then why we write this getfirsttname and getlastname functions .. ???
here is my code please have eye ..
i want to call the get last name function,, but this will generate errors ..
i really need your comments here ..

Thank you For Your Consideration .. !!!!

#include <iostream>
#include <string>
using namespace std;
class personType
{
public:
	void print() const;
	void setName(string first, string last);
	string getFirstName() const;
	string getLastName() ; //  const is removed from here. !!
	personType(string first, string last);
	personType();
private:
	string firstName, lastName;
};
void personType::print() const
{
	cout << firstName << " " << lastName;
}

void personType::setName(string first, string last)
{
	firstName = first;
	lastName = last;
}

string personType::getFirstName() const
{
	return firstName;
}

string personType::getLastName() 
{
	return lastName;
}

// Here Comes The Constructor Turn
personType::personType(string first, string last)
{
	firstName = first;
	lastName = last;
}
int main()
{
	personType p;
	p.setName("Nerdy", "Usman");
	p.print();
	cout << p.getLastName();	// What The heck. ?!
	system("pause");
	return 0;
}

推荐答案

你只是忘了定义(实现)没有参数的构造函数(编译器不会自动提供默认构造函数)如果你定义了至少一个)。

添加,例如

You simply forgot to define (implement) the costructor without arguments (compiler does not automatically provide the default constructor if you have defined at least one).
Add, for instance
personType::personType()
{
}


这篇关于调用“cout”的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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