您的位置:首页 > 编程语言 > C语言/C++

C++中的小错误

2016-04-15 22:14 197 查看
最近开始学习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>

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

" "引用的是你程序目录的相对路径中的头文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: