注册 登录
编程论坛 VC++/MFC

哪位能手帮下

guxinghan 发布于 2010-10-10 21:23, 786 次点击
//stdafx.h
#include <iostream>
#include<string>
#include <Emp.h>
using namespace std;

//Emp.h
using namespace std;
class CEmp  
{
public:
    CEmp();
    virtual~CEmp(){delete[] name;}
    void set_name(char *);
        void set_age(short a){age=a;};
        void set_salary(float s){salary=s;};
        void print();
        
private: char *name;
         short age;
         float salary;
}
//Emp.cpp
#include"stdafx.h"
#include "Emp.h"

CEmp::CEmp()
{
    name=0;
    age=0;
    salary=0;

}
void CEmp::set_name(char *n)
{
name=new char(strlen(n)+1);
strcpy(name,n);
}
void CEmp::print()
{
cout<<"Name:"<<name;
cout<<"Age:"<<age;
cout<<"Salary:"<<salary<<endl;
}

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


int main(int argc,char* argv[])
{
    char *name=NULL;
    short age=0;
    float salary=0;
    name=new char[30];
    CEmp emp[3];
    for(int i=0;i<3;i++)
    {
        cout<<"输入第"<<i+1<<"员工的信息"<<endl;
        cout<<"姓名:";
        cin>>name;
        cout<<"年龄:";
        cin>>age;
        cout<<"工资:";
        cin>>salary;
        emp[i].set_name(name);
        emp[i].set_age(age);
        emp[i].set_salary(salary);
        emp[i].print();
    }
    return 0;
}
错误f:\工具\visual c++6.0\msdev98\myprojects\009\stdafx.h(17) : fatal error C1083: Cannot open include file: 'Emp.h': No such file or directory
帮忙看下。为什么打不开Emp.h 头文件啊
12 回复
#2
m21wo2010-10-10 22:02
程序代码:
Emp.h
#ifndef EMP_H
#define EMP_H

class CEmp
{
public:
    CEmp();
    ~CEmp(){delete[] name;}
    void set_name(char *);
    void set_age(short a){age=a;};
    void set_salary(float s){salary=s;};
    void print();

private: char *name;
         short age;
         float salary;
};                                       //没加;
#endif

Emp.cpp

#include <iostream>
#include <string>
#include "Emp.h"
using namespace std;

CEmp::CEmp()
{
    name=0;
    age=0;
    salary=0;

}
void CEmp::set_name(char *n)
{
name=new char(strlen(n)+1);
strcpy(name,n);
}
void CEmp::print()
{
cout<<"Name:"<<name;
cout<<"Age:"<<age;
cout<<"Salary:"<<salary<<endl;
}


int main(int argc,char* argv[])
{
    char *name=NULL;
    short age=0;
    float salary=0;
    name=new char[30];
    CEmp emp[3];
    for(int i=0;i<3;i++)
    {
        cout<<"输入第"<<i+1<<"员工的信息"<<endl;
        cout<<"姓名:";
        cin>>name;
        cout<<"年龄:";
        cin>>age;
        cout<<"工资:";
        cin>>salary;
        emp[i].set_name(name);
        emp[i].set_age(age);
        emp[i].set_salary(salary);
        emp[i].print();
    }
    return 0;
}


#3
guxinghan2010-10-11 13:40
stdafx.h需要放些什么头文件在里面.难道不要嘛?加个qq好不?我的976611409。
#4
guxinghan2010-10-11 13:54
具体告诉我怎么放这些文件怎么就是不行啊。汗
#5
hahayezhe2010-10-11 14:08
#include "Emp.h"  不是 #include <Emp.h>
"" <>是有区别的 寻找的起始路径不同! <>是不在当前目录里寻找的
除非你在项目属性里添加了头文件的寻找路径
#6
guxinghan2010-10-11 16:25
可是改成"Emp.h"还是说不正确啊。
#7
hahayezhe2010-10-11 17:09
那里不正确 你可以一次说明白点吗?
你都做过什么尝试 都出现些什么错误
你自己的判断错误在那里?
别老让别人猜行不?
#8
guxinghan2010-10-11 18:02
改成:#include"Emp.h"后:
f:\工具\visual c++6.0\msdev98\myprojects\009\stdafx.h(18) : error C2143: syntax error : missing ';' before 'using'
加了;后:f:\工具\visual c++6.0\msdev98\myprojects\009\stdafx.h(17) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
f:\工具\visual c++6.0\msdev98\myprojects\009\stdafx.h(18) : error C2143: syntax error : missing ';' before 'using'
执行 cl.exe 时出错.帮忙下谢谢了
#9
m21wo2010-10-11 19:02
你的class Emp {};这个分号没加啊!
#10
guxinghan2010-10-12 15:37
为什么要加分号啊。加了试了也不行。我是这样操作的。首先点击插入,插入类,命名CEmp,在Emp.h头文件里编入上面的那些程序。接着在Emp.cpp写入程序。再在stdafx.h头文件中加入一些需要包含的头文件。再text.cpp写入上述程序。我是按照书本上一字不漏抄下来的。怎么就是运行不正确。是 哪里操作不正确吗?帮忙下。谢谢了。我用的是visual c++ 6.0版本编写的。
#11
myth_feng2010-10-12 18:07
#include <iostream>
#include<string>

using namespace std;

//Emp.h
using namespace std;
class CEmp
{
public:
    CEmp();
    virtual~CEmp(){delete[] name;}
    void set_name(char *);
        void set_age(short a){age=a;};
        void set_salary(float s){salary=s;};
        void print();

private: char *name;
         short age;
         float salary;
};
//Emp.cpp


CEmp::CEmp()
{
    name=0;
    age=0;
    salary=0;

}
void CEmp::set_name(char *n)
{
name=new char(strlen(n)+1);
strcpy(name,n);
}
void CEmp::print()
{
cout<<"Name:"<<name;
cout<<"Age:"<<age;
cout<<"Salary:"<<salary<<endl;
}

//text.cpp



int main(int argc,char* argv[])
{
    char *name=NULL;
    short age=0;
    float salary=0;
    name=new char[30];
    CEmp emp[3];
    for(int i=0;i<3;i++)
    {
        cout<<"输入第"<<i+1<<"员工的信息"<<endl;
        cout<<"姓名:";
        cin>>name;
        cout<<"年龄:";
        cin>>age;
        cout<<"工资:";
        cin>>salary;
        emp[i].set_name(name);
        emp[i].set_age(age);
        emp[i].set_salary(salary);
        emp[i].print();
    }
    system("Pause");
    return 0;
}
#12
yangang22010-10-12 18:52
除去以上他们说的错误外,你还犯了一个典型的错误,就是类的定义重复了。你的CEmp被定义了两次,难道你不知道吗?是的,它的确比较隐晦,不容易察觉。首先看你的Emp.cpp文件,找到第一个预定义处理#include"stdafx.h",编译时自然转到stdafx.h头文件中,它的内容是:
#include <iostream>
#include<string>
#include "Emp.h"
using namespace std;
特别注意#include "Emp.h",它又使其转到Emp.h头文件中,它的内容即是对CEmp类进行定义,定义完后编译using namespace std;然后就又自然转到Emp.cpp文件中,编译#include"stdafx.h"后的内容,它是什么?不巧它就是#include "Emp.h",编译器又转到Emp.h中,这就很明显了吧!CEmp确实定义了两次。

解决办法:
在Emp.h即类CEmp的定义文件中添加如下预定义宏:
#ifndef Emp_h
#define Emp_h
class CEmp()
{
......
};
#endif
程序就OK了

还提醒一下就是多个源文件.cpp进行编译时是分别独立编译的,如你的text.cpp与Emp.cpp独立编译,你的text.cpp没有错误,既然你说的是抄的书上的,我想你肯定是在Emp.cpp中抄多了那句#include "Emp.h"
我把我给你修改后的传上来
只有本站会员才能查看附件,请 登录




[ 本帖最后由 yangang2 于 2010-10-12 18:55 编辑 ]
#13
m21wo2010-10-12 18:59
就想我那么写不就ok 了!你怎么弄啊!
1