簡體   English   中英

C ++類的getter在main中不返回任何值,但是在類中返回嗎?

[英]C++ class getter not returning any value in main, but does in the class?

我希望有人能指出我的方向,為什么在聲明了setter的函數之外使用getter函數時,似乎無法正確訪問它。

我正在讀取文件,使用getline函數存儲每一行​​的變量,然后將消息分配給Message.h中的私有變量。

當我在readfile()函數中m1.getmessage() ,它輸出的結果非常好,輸出正確(我的文本文件中的消息行),但是它只是在main() 給出了空白輸出 我已經嘗試了好幾個小時了,我一直在閱讀有關局部變量的信息,但據我所知,變量已經設置,並且在公共函數中,因此我看不到我要去哪里了 在我凌晨4點到達酒庄之前,任何幫助將不勝感激。

Message.h

#include <iostream>

class Message {
private:
std::string message;
std::string cipher;


public:
    void readFile();

    void setMessage(std::string msg) {
        message = msg;
    }
    void setCipher(std::string ciph) {
        cipher = ciph;
    }

    std::string getMessage() {
        return message;
    }
    std::string getCipher() {
        return cipher;
    }
};

Message.cpp

#include "Message.h"
#include <fstream>
#include <iostream>

void Message::readFile()    {
    std::string fileUsername;
    std::string fileForename;
    std::string fileSurname;
    std::string fileAge;
    std::string fileTime;
    std::string fileHour;
    std::string fileMin;
    std::string fileSec;
    std::string fileCipher;
    std::string fileMessage;
    Message m1;
    std::fstream file;
    std::string filename;
    std::cout << "Please enter file name: " << std::endl;
    getline(std::cin, filename);
    file.open(filename);
    if (file.is_open()) {
        std::cout << "File opened" << std::endl;
    } else {
        std::cout << "Wrong file name" << std::endl;
    }
    while(file.is_open()) {

        getline(file, fileUsername);
        getline(file, fileForename);
        getline(file, fileSurname);
        getline(file, fileAge);
        getline(file, fileHour, ':');
        getline(file, fileMin, ':');
        getline(file, fileSec);
        getline(file, fileCipher);
        getline(file, fileMessage);
        file.close();
    }
    m1.setMessage(fileMessage);
    m1.setCipher(fileCipher);
    m1.getMessage();

};

Main.cpp的

#include <iostream>
#include <iomanip>
#include <fstream>
#include "Message.h"
#include "Caesar.h"
#include "XOR.h"


int main() {

    Message m1;
    m1.readFile();
    std::cout << m1.getMessage();


    return 0;
}

主函數中的cout不返回任何內容,但是如果我將其傳輸到m1.readfile()中,它將完美輸出變量。

這是我第一次嘗試面向對象的編程,這絕對是我的弱點。 在此先感謝您的任何建議。

Message::readFile()函數中,您不是在當前對象上調用setMessagesetCipher函數,而是在局部變量m1上調用。 局部變量在函數末尾被丟棄,消息和密碼最終不會得到保存。 您應該打電話給

setMessage(fileMessage);
setCipher(fileCipher);
getMessage();

代替

m1.setMessage(fileMessage);
m1.setCipher(fileCipher);
m1.getMessage();

這將更新當前對象的消息和密碼變量,然后可以從main函數中打印getMessage()

您在readfile中創建一個局部變量m1。 然后在函數結束時將其丟棄。 可能您想返回對象或設置當前實例

您不必在該類的成員函數中創建對象

我的建議:

  • 忽略/刪除Message m1; readFile()
  • 直接將功能稱為

    setMessage(fileMessage); setCipher(fileCipher); getMessage();

您可以使用 指向您當前正在處理的對象的指針。 像下面

this->setMessage(fileMessage);

要么

(*this).setMessage(fileMessage);

暫無
暫無

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

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