写了几段代码,居然不能编译.请大家帮小弟我看看
写了几段代码,居然不能编译.请大家帮我看看
头文件:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using namespace std;
namespace demo
{
class Person
{
public:
Person();
~Person();
virtual int Age();
virtual string Name();
virtual char Sex();
private:
int age;
string name;
char sex;
};
//class Student
class Student:protected Person
{
public:
string School();
string ClassName();
private:
string school;
string className;
};
}
#endif
--------------------------------------
Student.cc类:
#include "person.h "
namespace demo
{
string Student::School()
{
return school;
}
string Student::ClassName()
{
return className;
}
}
--------------------------------------
Person(基类)类:
#include "person.h "
using namespace demo;
Person::Person()
{
}
Person::~Person()
{}
int Person::Age()
{
return age;
}
string Person::Name()
{
return name;
}
char Person::Sex()
{
return sex;
}
-------------------------------------
main.cc
#include "person.h "
using namespace demo
int main()
{
Student *stu = new Student();
dtu-> Age = 10;
cout < < dtu-> Age;
}
编译有很多错误.....
谢谢您的耐心
------解决方案--------------------
1.main.cc中using namespace demo后少;号
2.dtu-> Age = 10;cout < < dtu-> Age; dut没定义,应该是stu吧
这两个还算是小错误
dtu-> Age = 10;
Age是函数,它不能赋值
另外.class Student:protected Person
所以,即使写cout < < dtu-> Age();Student中的Age也不能被访问
建议楼主多看一下C++的类的基础部分
头文件:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using namespace std;
namespace demo
{
class Person
{
public:
Person();
~Person();
virtual int Age();
virtual string Name();
virtual char Sex();
private:
int age;
string name;
char sex;
};
//class Student
class Student:protected Person
{
public:
string School();
string ClassName();
private:
string school;
string className;
};
}
#endif
--------------------------------------
Student.cc类:
#include "person.h "
namespace demo
{
string Student::School()
{
return school;
}
string Student::ClassName()
{
return className;
}
}
--------------------------------------
Person(基类)类:
#include "person.h "
using namespace demo;
Person::Person()
{
}
Person::~Person()
{}
int Person::Age()
{
return age;
}
string Person::Name()
{
return name;
}
char Person::Sex()
{
return sex;
}
-------------------------------------
main.cc
#include "person.h "
using namespace demo
int main()
{
Student *stu = new Student();
dtu-> Age = 10;
cout < < dtu-> Age;
}
编译有很多错误.....
谢谢您的耐心
------解决方案--------------------
1.main.cc中using namespace demo后少;号
2.dtu-> Age = 10;cout < < dtu-> Age; dut没定义,应该是stu吧
这两个还算是小错误
dtu-> Age = 10;
Age是函数,它不能赋值
另外.class Student:protected Person
所以,即使写cout < < dtu-> Age();Student中的Age也不能被访问
建议楼主多看一下C++的类的基础部分