读书人

使用VC++6.0时用#ifdef出错解决思路

发布时间: 2012-03-06 20:47:55 作者: rapoo

使用VC++6.0时用#ifdef出错
我写了个程序发现用了#ifdef后就编译出错,删了后就行了。请教是怎么回事。是我的编译器有问题吗?使用的是VC++6.0。创建的是Win32 console Application
以下是代码

头文件:
#ifdef PERSONTYPE_H
#define PERSONTYPE_H

#include<string>
using namespace std;

class personType
{
public:
void print() const;
void setName(string first,string last);
void getName(string& ,string& );
personType(string="",string="");
private:
string firstName;
string lastName;
};
#endif




实现文件:
#include"personType.h"

#include<iostream>
#include<string>


using namespace std;

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


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


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


void personType::print() const
{
cout<<firstName<<" "<<lastName<<endl;
}




main函数:
#include"personType.h"
#include<iostream>
using namespace std;
int main()
{
personType person1("dfaff","dsa");
person1.print();
return 0;
}


[解决办法]
应该是#ifndef才继续#define的。
按你写的不是一定要重复包含咯。

读书人网 >C++

热点推荐