我使用的代码块V16 *类成分混乱
我得到这个错误,我想不出什么似乎是这个问题:
Person.cpp:17:62:注:不匹配类型'const std :: basic_string < _CharT,_Traits,_Alloc>'and'void' std :: cout < <“Birthdate:”< < BirthdateObj.showBirthDate(); ^ 过程终止状态1(0分钟,0秒) 1误差(S),0警告(S)(0分钟,0秒)
这里是我的简单的程序:
Person.h
#ifndef PERSON_H
#define PERSON_H
#include <Birthdate.h>
class Person
{
public:
Person(int age, Birthdate BirthdateObj);
virtual ~Person();
void showPersonInformation();
private:
int age;
Birthdate BirthdateObj;
};
#endif // PERSON_H
Person.cpp
#include "Person.h"
#include <iostream>
Person::Person(int age, Birthdate BirthdateObj) : age(age), BirthdateObj(BirthdateObj)
{
//ctor
}
Person::~Person()
{
//dtor
}
void Person::showPersonInformation()
{
std::cout << "Current age: " << age << std::endl;
std::cout << "Birthdate: " << BirthdateObj.showBirthDate();
}
乙irthdate.h
#ifndef BIRTHDATE_H
#define BIRTHDATE_H
class Birthdate
{
public:
Birthdate(int day, int month, int year);
virtual ~Birthdate();
void showBirthDate();
private:
int day, month, year;
};
#endif // BIRTHDATE_H
Birthdate.cpp
#include "Birthdate.h"
#include <iostream>
Birthdate::Birthdate(int day, int month, int year) : day(day), month(month), year(year)
{
//ctor
}
Birthdate::~Birthdate()
{
//dtor
}
void Birthdate::showBirthDate()
{
std::cout << day << "/" << month << "/" << year;
}
的main.cpp
#include <iostream>
#include "Birthdate.h"
#include "Person.h"
int main()
{
Birthdate BirthdateObj(7, 16, 1995);
//Birthdate *pBirthdateObj = &BirthdateObj;
Person PersonObj(20, BirthdateObj);
Person *pPersonObj = &PersonObj;
pPersonObj->showPersonInformation();
//pBirthdateObj->showBirthDate();
return 0;
}
您似乎对函数及其返回值的工作方式存在一些误解。考虑选择一本关于C++的书籍(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list),以确保你没有从有疑问的材料中学习。 –
BirthdateObj.showBirthDate()返回无法传递给std :: cout的void。顺便说一句,你知道你可以调用PersonObj.showPersonInformation(),对吧? – stijn
@stijn你好,是的,我现在明白了。无论如何对我来说(只为我或其他人),指向一个对象,并通过 - >访问它的成员是美丽的大声笑。那么你可以告诉我,如果这是一个不好的做法或不是寿。 – Fur