类放在头文件里怎么不行

小马哥哥哥 2008-12-17 08:29:44
刚开始学习C++ ,搞不懂为什么将类放在头文件里不能运行!代码已经编好了,放在附件里面,将.H文件注销掉就能运行出正确结果!可是我想把同样的代码放在头文件里,该怎么改?
还有个问题就是谁有成型的C++小程序,我下载下来模仿一下!要有类的继承和派生,和多个.h和.cpp文件的相互调用之类的……
...全文
221 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
真不好意思!我没仔细看
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
--------------------Configuration: 基类和派生 - Win32 Debug--------------------
Compiling...
Person_main.cpp
d:\我的程序\继承和派生\person.h(60) : error C2143: syntax error : missing ';' before '<<'
d:\我的程序\继承和派生\person.h(60) : error C2501: 'cout' : missing storage-class or type specifiers
d:\我的程序\继承和派生\person.h(60) : error C2371: 'cout' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : see declaration of 'cout'
d:\我的程序\继承和派生\person.h(60) : error C2143: syntax error : missing ';' before '<<'
d:\我的程序\继承和派生\person.h(61) : error C2143: syntax error : missing ';' before '<<'
d:\我的程序\继承和派生\person.h(61) : error C2501: 'cout' : missing storage-class or type specifiers
d:\我的程序\继承和派生\person.h(61) : error C2371: 'cout' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : see declaration of 'cout'
d:\我的程序\继承和派生\person.h(61) : error C2143: syntax error : missing ';' before '<<'
d:\我的程序\继承和派生\person.h(61) : error C2143: syntax error : missing ';' before 'string'
d:\我的程序\继承和派生\person.h(61) : error C2059: syntax error : ')'
d:\我的程序\继承和派生\person.h(62) : error C2143: syntax error : missing ';' before '<<'
d:\我的程序\继承和派生\person.h(62) : error C2501: 'cout' : missing storage-class or type specifiers
d:\我的程序\继承和派生\person.h(62) : error C2371: 'cout' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : see declaration of 'cout'
d:\我的程序\继承和派生\person.h(62) : error C2143: syntax error : missing ';' before '<<'
d:\我的程序\继承和派生\person.h(63) : error C2143: syntax error : missing ';' before '}'
d:\我的程序\继承和派生\person.h(63) : error C2143: syntax error : missing ';' before '}'
d:\我的程序\继承和派生\person.h(63) : error C2143: syntax error : missing ';' before '}'
d:\我的程序\继承和派生\person.h(68) : error C2143: syntax error : missing ';' before 'private'
d:\我的程序\继承和派生\person.h(69) : error C2143: syntax error : missing ';' before '}'
d:\我的程序\继承和派生\person.h(69) : error C2143: syntax error : missing ';' before '}'
d:\我的程序\继承和派生\person.h(69) : error C2143: syntax error : missing ';' before '}'
d:\我的程序\继承和派生\person.h(70) : error C2011: 'Teacher' : 'class' type redefinition
d:\我的程序\继承和派生\person.h(85) : error C2084: function '__thiscall Teacher::Teacher(char *,int,char,char *,int)' already has a body
d:\我的程序\继承和派生\person.h(90) : error C2011: 'Student' : 'class' type redefinition
d:\我的程序\继承和派生\person.h(103) : error C2084: function '__thiscall Student::Student(char *,int,char,char *,char *)' already has a body
d:\我的程序\继承和派生\person_main.cpp(66) : error C2264: 'Teacher::Teacher' : error in function definition or declaration; function not called
d:\我的程序\继承和派生\person_main.cpp(67) : error C2264: 'Student::Student' : error in function definition or declaration; function not called
Error executing cl.exe.

继承和派生.exe - 27 error(s), 0 warning(s)
fibbery 2008-12-17
  • 打赏
  • 举报
回复
你把错误贴出来。
fibbery 2008-12-17
  • 打赏
  • 举报
回复
我把注释的部分保存为person.h后,编译执行都没有问题,环境是VC6
执行结果:
E:\MyDocs\debug>test
姓 名:章广
性 别:男
年 龄:38
工作单位:电气学院
月 薪:2300

学 号:02035003
姓 名:刘薇
性 别:女
年 龄:22
班 级:能动 01
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fibbery 的回复:]
一般类都是放在头文件里的。

C/C++ code//myclass.h
class CMyClass
{
....
};
//myclass.cpp
CMyClass::CMyClass()
{
...
}
CMyClass::~CMyClass()
{
...
}
....



如果你想把类的实现也放在头文件,那么最好如下:

C/C++ codeclass CMyClass
{
CMyClass()
{
...
}
~CMyClass()
{
...
}
...
};
[/Quote]

#include <iostream.h>
#include <string>
#include "person.h"
//class Person
//{
//public:
// void Register(char*name,int age,char sex)
// {
// strcpy(m_strName,name);
// m_nSex=(sex=='m'?0:1);
// m_nAge=age;
// }
// void ShowMe()
// {
// cout < <"姓 名:" < <m_strName < <endl;
// cout < <"性 别:" < <(m_nSex==0?"男":"女") < <endl;
// cout < <"年 龄:" < <m_nAge < <endl;
// }
//protected:
// char m_strName[10];
// int m_nSex;
// int m_nAge;
//private:
//};
//class Teacher:public Person
//{
//public:
// Teacher(char*name,int age,char sex,char*dept,int salary);
// void ShowMe()
// {Person::ShowMe();
// cout < <"工作单位:" < <m_strDept < <endl;
// cout < <"月 薪:" < <m_fSalary < <endl < <endl;
// }
//protected:
//private:
// char m_strDept[20];
// int m_fSalary;
//};
//Teacher::Teacher(char*name,int age,char sex,char*dept,int salary)
//{
// Register(name,age,sex);
// strcpy(m_strDept,dept);
// m_fSalary=salary;
//}
//class Student:public Person
//{char m_strID[12];
//char m_strClass[12];
//public:
// Student(char*name,int age,char sex,char*ID,char*Class);
// void ShowMe()
// {cout < <"学 号:" < <m_strID < <endl;
// Person::ShowMe();
// cout < <"班 级:" < <m_strClass < <endl;
//
// }
//};
//Student::Student(char*name,int age,char sex,char*ID,char*Class)
//{
// Register(name,age,sex);
// strcpy(m_strID,ID);
// strcpy(m_strClass,Class);
//}

void main()
{
Teacher emp1("章广",38,'m',"电气学院",2300);
Student std1("刘薇",22,'f',"02035003","能动 01");
emp1.ShowMe();
std1.ShowMe();
}
程序是这样的!把注掉的部分放到头文件里就不行了,我该怎么改?
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
谢谢,您辛苦了
xuruichen 2008-12-17
  • 打赏
  • 举报
回复
下载了,正在看,这么多,不一定搞的定。
xuruichen 2008-12-17
  • 打赏
  • 举报
回复
把你的代码贴出来,
类放在头文件中声明,放在同名的.cpp文件中进行定义。
结合main(),才可以运新。单独一个类怎么运行?
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
刚知到原来可以这么提问
nullah 2008-12-17
  • 打赏
  • 举报
回复

//complex.h
#ifndef _COMPLEX_H
#define _COMPLEX_H

#include <iostream>
using namespace std;

class complex
{
public:
complex();
private:
int i;
};

#endif

//complex.cpp
#include "complex.h"

complex::complex()
{
cout << "complex::complex()" << endl;
i = 10;
cout << i << endl;
}

//main.cpp
#include <iostream>
#include "complex.h"
using namespace std;

int main()
{
complex a;
return 0;
}

/*output:
**complex::complex()
**10
*/请按任意键继续. . .
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <string>
#include "person.h"
//class Person
//{
//public:
// void Register(char*name,int age,char sex)
// {
// strcpy(m_strName,name);
// m_nSex=(sex=='m'?0:1);
// m_nAge=age;
// }
// void ShowMe()
// {
// cout<<"姓 名:"<<m_strName<<endl;
// cout<<"性 别:"<<(m_nSex==0?"男":"女")<<endl;
// cout<<"年 龄:"<<m_nAge<<endl;
// }
//protected:
// char m_strName[10];
// int m_nSex;
// int m_nAge;
//private:
//};
//class Teacher:public Person
//{
//public:
// Teacher(char*name,int age,char sex,char*dept,int salary);
// void ShowMe()
// {Person::ShowMe();
// cout<<"工作单位:"<<m_strDept<<endl;
// cout<<"月 薪:"<<m_fSalary<<endl<<endl;
// }
//protected:
//private:
// char m_strDept[20];
// int m_fSalary;
//};
//Teacher::Teacher(char*name,int age,char sex,char*dept,int salary)
//{
// Register(name,age,sex);
// strcpy(m_strDept,dept);
// m_fSalary=salary;
//}
//class Student:public Person
//{char m_strID[12];
//char m_strClass[12];
//public:
// Student(char*name,int age,char sex,char*ID,char*Class);
// void ShowMe()
// {cout<<"学 号:"<<m_strID<<endl;
// Person::ShowMe();
// cout<<"班 级:"<<m_strClass<<endl;
//
// }
//};
//Student::Student(char*name,int age,char sex,char*ID,char*Class)
//{
// Register(name,age,sex);
// strcpy(m_strID,ID);
// strcpy(m_strClass,Class);
//}

void main()
{
Teacher emp1("章广",38,'m',"电气学院",2300);
Student std1("刘薇",22,'f',"02035003","能动 01");
emp1.ShowMe();
std1.ShowMe();
}
程序是这样的!把注掉的部分放到头文件里就不行了,我该怎么改?
小马哥哥哥 2008-12-17
  • 打赏
  • 举报
回复
哦!可是代码太多了,不太好往这上面发表啊
fibbery 2008-12-17
  • 打赏
  • 举报
回复
一般类都是放在头文件里的。
//myclass.h
class CMyClass
{
....
};
//myclass.cpp
CMyClass::CMyClass()
{
...
}
CMyClass::~CMyClass()
{
...
}
....


如果你想把类的实现也放在头文件,那么最好如下:
class CMyClass
{
CMyClass()
{
...
}
~CMyClass()
{
...
}
...
};
ligang19840226 2008-12-17
  • 打赏
  • 举报
回复
找本c++的书,或者google下
ligang19840226 2008-12-17
  • 打赏
  • 举报
回复
你这下载要分啊??

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧