栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

个人c++ 错误记录

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

个人c++ 错误记录

1.类定义输入运算符 >> 时候,代码最后应该检测 输入流是否为有效,如果无效,应该把输入对象置于默认初始化状态。

    定义>>时候,类必须是非常量引用。定义<<,最好是常量引用。

     定义的时候不能加friend.

2.关于日期输入的正确格式:

Date & Date::operator=(const string &date)
{
istringstream is(date);
char ch1,ch2;
is >> year >> ch1 >> month >> ch2 >> day; 
if(!is || ch1 != '-' || ch2 != '-' )
	throw invalid_argument("Bad date");
if(month < 1 || month > 12 || day < 1 || day > 21)
	throw invalid_argument("Bad date");
return *this;
}

3.

#include 
#include 
using namespace std;
class Test
{
	int &fun()const
	{
	return a;
	}

	private:
		int a;


};
int main()
{
Test t1;


	return 0;
}

 

binding reference of type ‘int&’ to ‘const int’ discards qualifiers

const成员函数不能返回类成员的非常量引用。因为有修改类成员的风险。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/311973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号