我在C ++中有三个类:Project,Supervisor,Date。我正在尝试使用合成,但是遇到以下错误消息时出现错误:
"Class Date
"Date" is not a nonstatic data member or base class of class "Project"".
此错误在Project.cpp的这一行上发生:
Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName)
: Date(sDay, sMonth, sYear),
: Supervisor(supervisorName)
完整代码:
Date.h:
#pragma once
class Date
{
public:
Date(int = 1, int = 1, int = 1);
void setDate(int, int, int);
void setDay(int);
void setMonth(int);
void setYear(int);
~Date();
private:
int day, month, year;
};
Date.cpp
#include "stdafx.h"
#include "Date.h"
Date::Date(int a, int b, int c)
{
setDate(a, b, c);
}
void Date::setDate(int d, int m, int y)
{
setDay(d);
setMonth(m);
setYear(y);
}
void Date::setDay(int da)
{
day = da;
}
void Date::setMonth(int mo)
{
month = mo;
}
void Date::setYear(int ye)
{
year = ye;
}
Date::~Date()
{
}
Supervisor.h:
#pragma once
class Supervisor
{
public:
Supervisor(char);
~Supervisor();
private:
char name;
};
Supervisor.cpp:
#include "stdafx.h"
#include "Supervisor.h"
Supervisor::Supervisor(char n)
{
name = n;
}
Supervisor::~Supervisor()
{
}
Project.h:
#pragma once
#include "Supervisor.h"
#include "Date.h"
class Project
{
public:
Project(char, int, int, int, char);
void setProject(char, int, int, int, char);
~Project();
private:
char pname;
Date startDate;
Supervisor supervisor;
};
Project.cpp:
#include "stdafx.h"
#include "Project.h"
#include "Supervisor.h"
#include "Date.h"
Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName)
: Date(sDay, sMonth, sYear),
: Supervisor(supervisorName)
{
setProject(projectName, sDay, sMonth, sYear, supervisorName);
}
void Project::setProject(char pN, int d, int m, int y, char sN)
{
pname = pN;
startDate = (d, m, y);
supervisor = sN;
}
Project::~Project()
{
}
答案 0 :(得分:2)
该错误消息是可以自我解释的,您正在使用语法将参数传递给类的基类的构造函数,但所使用的类名称不是当前类的基类。
要初始化类成员,请使用成员名称而不是其类:
Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName)
: statrtDate(sDay, sMonth, sYear),
: supervisor(supervisorName)
{
setProject(projectName, sDay, sMonth, sYear, supervisorName);
}