を使用しています
:
Person.cppPerson.cpp:17:62:注:不一致タイプ 'const std :: basic_string < _CharT、_Traits、_Alloc>'および 'void' std :: cout < < "生年月日:" < < BirthdateObj.showBirthDate(); ^ プロセスはステータス1で終了(0分(S)、0秒(S)) 1エラー(S)、0警告(S)(0分(S)、0秒(S))
#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();
}
B
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
:
はここに私の単純なプログラムです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