多个文件的类使用.h .cpp main.cpp

时间:2014-11-20 08:56:00

标签: c++ class debugging constructor header

尝试制作Tamagotchi程序,但编译器将未定义的引用抛给' Tamagotchi :: age()错误

理想情况下,此代码将返回Tamagotchi的年龄,该年龄应该由类的构造函数初始化为0。

我显然在某个地方搞砸了,但我不知道在哪里,如果有人看到了哪里,可以帮助我理解那将是伟大的!

此外,如果你看到编码习惯不好的话,我是新手,我希望改进,所以欢迎任何帮助。

编辑:哎呀我忘了复制并粘贴在类中的函数定义中。它们在那里,但我仍然遇到编译器错误。

//tamagotchi.cpp
#include "tamagotchi.h"
#include <string>


/* return of Tamagotchi information */
std::string Tamagotchi::name() {return myName;}
int Tamagotchi::age() {return myAge;}
int Tamagotchi::happiness() {return myHappiness;}
int Tamagotchi::hunger() {return myHunger;}
bool Tamagotchi::rIsSick() {return isSick;}    

-

//tamagotchi.h
#ifndef TAMAGOTCHI_H
#define TAMAGOTCHI_H
#include <string>


class Tamagotchi
{
public:
        /* initialization of default for tamagotchi */
        Tamagotchi() 
        : myName ("Default"),
          myAge ( 0 ),
          myHappiness ( 0 ),
          myHunger ( 0 ),
          isSick ( false ) { }

/* returning tamagotchi variables */
        std::string name();
        int age();
        int happiness();
        int hunger();
        bool rIsSick();

private:
        std::string myName;
        int myAge;// defined from 0 - 50 based on hours
        int myHappiness;// defined from 0 - 10
        int myHunger; // defined from 0 - 10, greater is hungrier
        bool isSick;// defines whether or not the Tamagotchi is sick

};

#endif

-

//main.cpp
#include "tamagotchi.h" // defines tamagotchi class
#include <string>
#include <iostream>


int main(){
    Tamagotchi myTamagotchi;
    std::cout << myTamagotchi.age();
    return 0;
}

由于

2 个答案:

答案 0 :(得分:1)

您必须在类声明中的头文件中声明您的访问器函数(或任何成员函数):

class Tamagotchi
{
public:
        /* initialization of default for tamagotchi */
        Tamagotchi() 
        : myName ("Default"),
          myAge ( 0 ),
          myHappiness ( 0 ),
          myHunger ( 0 ),
          isSick ( false ) { }
private:
        std::string myName;
        int myAge;// defined from 0 - 50 based on hours
        int myHappiness;// defined from 0 - 10
        int myHunger; // defined from 0 - 10, greater is hungrier
        bool isSick;// defines whether or not the Tamagotchi is sick
public:
    std::string name();
    int age();
    int happiness();
    int hunger();
    bool rIsSick();
};

一些提示:声明不将对象的状态修改为const的成员函数很有用,如下所示:

std::string name() const;

您还必须相应地修改cpp文件中的定义:

std::string Tamagotchi::name() const {...}

我还建议您通过const引用返回容器对象,例如std :: string:

const std::string& name() const;

同样,您必须修改cpp文件中的定义:

const std::string& Tamagotchi::name() const { return myName; }

如果按值返回值,则始终会创建返回字符串的副本,这可能会导致性能降低。通过const引用返回可以避免这种情况。但这仅适用于容器和其他大型对象。像原始类型(int,float,bool等)和小类实例这样的简单事物可以通过值返回,几乎不需要任何费用。

答案 1 :(得分:1)

编译器是正确的...您定义的类不包含函数定义,只包含构造函数和一些私有成员变量。在.cpp文件中抛出方法是不够的。如果方法不在类定义中,那么就应用程序的其余部分而言,它不存在!

class Tamagotchi
{
public:
        /* initialization of default for tamagotchi */
        Tamagotchi() 
        : myName ("Default"),
          myAge ( 0 ),
          myHappiness ( 0 ),
          myHunger ( 0 ),
          isSick ( false ) { }

        std::string name();
        int age();
        int happiness();
        int hunger();
        bool rIsSick();

private:
        std::string myName;
        int myAge;// defined from 0 - 50 based on hours
        int myHappiness;// defined from 0 - 10
        int myHunger; // defined from 0 - 10, greater is hungrier
        bool isSick;// defines whether or not the Tamagotchi is sick
};   
相关问题