C++中的小错误

最近开始学习C++,调试代码过程中,发现了一些小错误,特此记录一下。

1、函数调用的错误。

<span style="font-family:Times New Roman;">include <stdlib.h>
#include <string.h>
#include<iostream>
using namespace std;
class Coordinate
{
public:
	int x;
	int y;
	void printX()
	{
		cout << x << endl;
	}
	void printY()
	{
		cout << y << endl;
	}
};
int main(void)
{
	Coordinate coor;
	coor.x = 10;
	coor.y = 20;
	coor.printX;
	coor.printY;
	system("pause");
	return 0;} </span>

这是弹出的错误提示为:

原来是我调用函数时候错误,没有加括号。

改正:Coordinate.printX();

Coordinate.printY();

2.cout无法识别。

<span style="font-family:Times New Roman;">#include <stdlib.h>
#include <string.h>
#include<iostream>
using namespace std;
class Student
{
public:
	void setName(string _name)
	{
		m_strName = _name;
	}
	string getName()
	{
		return m_strName;
	}
	void setGender(string _gender)
	{
		m_strGender = _gender;
	}
	string getGender()
	{
		return m_strGender;
	}
	int getScore()
	{
		return m_iScore;
	}
	void initScore()
	{
		m_iScore = 0;
	}
	void study(int _score)
	{
		m_iScore += _score; // m_iScore= m_iScore+_score
	}
private:
	string m_strName;
	string m_strGender;
	int m_iScore;
};
int main(void)
{
	Student stu;
	stu.initScore();
	stu.setName("zhangsan");
	stu.setGender("女");
	stu.study(5);
	stu.study(3);
	cout << stu.getName() << "" << stu.getGender() << "" << stu.getScore() << endl;
	system("pause");
	return 0;
}</span>

错误原因:函数头部声明的string.h 与string有区别。

解释:http://www.cnblogs.com/frustrate2/archive/2012/12/03/2799341.html

引用:

一般一个C++的老的带“.h”扩展名的库文件,比如iostream.h在新标准后的标准库中都有一个不带“.h”扩展名的相对应,区别除了后者的好多改进之外,还有一点就是后者的东东都塞进了“std”名字空间中。
但唯独string特别。
问题在于C++要兼容C的标准库,而C的标准库里碰巧也已经有一个名字叫做“string.h”的头文件,包含一些常用的C字符串处理函数,比如楼主提到的strcmp
这个头文件跟C++string类半点关系也没有,所以<string>并非<string.h>升级版本他们是毫无关系的两个头文件。
要达到楼主的目的,比如同时:
#include <string.h>
#include <string>
using namespace std;
或者
#include <cstring>
#include <string>
其中<cstring>是与C标准库的<string.h>相对应,但裹有std名字空间的版本

3.引用头文件问题。

#include“Teacher.h”

#include<string>

< >引用的是编译器的类库路径里面的头文件

" "引用的是你程序目录的相对路径中的头文件





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值