读书人

重载流插入符的有关问题 郁闷~

发布时间: 2012-02-12 17:16:33 作者: rapoo

重载流插入符的问题 郁闷~~
我用的vs2005 下面是我的代码 遇到一个极其郁闷的问题 不知道哪出错了
帮忙看下 谢谢

///////////////////////stdafx.h///////////////////////////////////////
#pragma once

#ifndef _WIN32_WINNT// 允许使用特定于 Windows XP 或更高版本的功能。
#define _WIN32_WINNT 0x0501// 将此值更改为相应的值,以适用于 Windows 的其他版本。
#endif

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string.h>
using namespace std;


///////////////student.h///////////////////////////////////////////////
#pragma once

class student
{

public:
student(void);
~student(void);
student(char*,int);
private:
int age;
char* name;
public:
int SetName(char*);
int SetAge(int age);
char* GetName(void);
int GetAge(void);

friend std::ostream &operator < <(std::ostream &,student &);
};

////////////////////student.cpp/////////////////////////////////////
#include "stdafx.h "
#include "student.h "

student::student(void)
{
name = new char[30];
}
student::student(char *name, int age)
{
this-> name = new char[20];
strcpy(this-> name,name);
this-> age=age;
}

student::~student(void)
{
delete []name;
}

int student::SetName(char*name)
{
strcpy(this-> name,name);
return 0;
}

int student::SetAge(int age)
{
this-> age = age;
return 0;
}

char* student::GetName(void)
{
return name;
}

int student::GetAge(void)
{
return age;
}

ostream &operator < <(ostream &ost,stduent &stu)
{
cout < <stu.age < <endl;
cout < <stu.name < <endl;
return ost;

}

/////////////////////////////operator.cpp/////////////////////////////

#include "stdafx.h "
#include "student.h "
int _tmain(int argc, _TCHAR* argv[])
{
student stu1( "zhansan ",20),stu2( "lisi ",30);
cout < <stu1;
cout < <stu2;
return 0;
}

//////////////////////////stdfx.cpp//////////////////////////////////////
#include "stdafx.h "


编译提示:
error C2061: 语法错误 : 标识符“stduent
error C2805: 二进制“operator < <”的参数太少
error C2065: “stu”: 未声明的标识符
error C2228: “.age”的左边必须有类/结构/联合 类型是“ 'unknown-type '”
error C2228: “.name”的左边必须有类/结构/联合 类型是“ 'unknown-type '”




[解决办法]
stduent拼写错啦。
cout < <stu.age < <endl;这个也应该是ost,不是cout

读书人网 >C++

热点推荐