2016-05-21 83 views
0

我使用的代码块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; 
} 
+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。由于cout无法将void输出到标准输出,因此这是您的错误来自哪里。改变这一行:

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

到:

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

或者,你可以的showBirthdate返回类型更改为string,并保持上述行为。

+0

你好,非常感谢你,我已经明白了。它有没有任何术语?我会选择你的帖子作为答案。 – Fur

+0

* term *是什么意思? – PcAF

+0

@PAFAF有没有什么办法可以让我不必制作一个类对象,只需制作一个指针呢? – Fur