2016-05-21 9 views
0
私はこのエラーを持って、問題になるように見えた私は何を把握することはできませんコードブロックV16の*

クラス組成混乱

を使用しています

Person.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))

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(); 
} 

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; 
} 
+0

関数とその戻り値がどのように機能するかについていくつかの誤解があるようです。疑わしい資料から学ぶことがないように、[C++に関する素晴らしい本](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)を参考にしてください。 –

+1

BirthdateObj.showBirthDate()は、std :: coutに渡すことができないvoidを返します。あなたはPersonObj.showPersonInformation()を呼び出すことができますか? – stijn

+0

@stijnこんにちは、はい私は今それを得ました。とにかく私のために(ちょうど私または多分他の人々のために)、オブジェクトへのポインタを持って - >経由でそのメンバーにアクセスするのは美しい笑です。あなたはそれが悪い練習かどうか私に教えてもらえます。 – Fur

答えて

0

あなたの誕生日の機能はvoidの代わりstringを返します。 coutvoidを標準出力に出力できないため、これがエラーの原因です。

std::cout << "Birthdate: " << BirthdateObj.showBirthDate(); 

:この行を変更し

また
std::cout << "Birthdate: "; 
BirthdateObj.showBirthDate(); 

、あなたがstringshowBirthdateの戻り値の型を変更しているとして上記の行を保つことができます。

+0

こんにちは、ありがとう、私は今それを考え出した。それには期間がありますか?あなたの投稿を回答として選択します。 – Fur

+0

*用語*の意味は? – PcAF

+0

@PcAFクラスオブジェクトを作成してポインタを作成する必要がない方法はありますか? – Fur