簡體   English   中英

多個文件.h .cpp main.cpp的類用法

[英]Class usage over multiple files .h .cpp main.cpp

嘗試制作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;
}

謝謝

您必須在類聲明的頭文件中聲明訪問器函數(或與此相關的任何成員函數):

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等)之類的簡單事物和小型類的實例幾乎可以免費按值返回。

編譯器是正確的...您定義的類不包含函數定義,僅包含構造函數和一些私有成員變量。 僅將方法扔到.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
};   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM