簡體   English   中英

C ++ getter不返回值(NULL)

[英]c++ getter not returning value (NULL)

當我嘗試cout get函數它不返回任何價值!

我的頭文件:

#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class Dog
{
private:
    string dogName = "Max";
public:
    Dog();
    void talking();
    void jumping();
    void setDogName(string newDogName);
    ///////thats the function /////
    string getDogName();
};

我的cpp文件:

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;


Dog::Dog() {
    cout << " Dog has been Created " << endl;

}
void Dog::setDogName(string newDogName)
{
    newDogName = dogName;
}
string Dog::getDogName()
{
    return dogName;
}

和我的main.cpp文件:

#include "stdafx.h"
#include <iostream>

#include "Dog.h"
#include "Cat.h"

int main()
{
    Dog ewila;

    ewila.setDogName("3wila");

    cout << ewila.getDogName();


    return 0;
}

我正在學習新的C ++,所以即使我嘗試鍵入ewila.getDogName();我也不知道發生了什么ewila.getDogName(); 就其本身而言,它什么也沒做,我試圖將setDogname()存儲在變量中以與getDogName()返回,也沒有什么我也不知道我在做什么錯 即時通訊使用Visual Studio 2017和我從 Visual c ++ 2015 MSBuild命令提示符 運行程序

問題出在行newDogName = dogName; 函數void Dog::setDogName(string newDogName) 您使用的assignment operator= )不正確。 這應該是dogName =newDogName;

因此,更正后的CPP文件將為:

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;


Dog::Dog() {
    cout << " Dog has been Created " << endl;
}
void Dog::setDogName(string newDogName)
{
    dogName = newDogName;
}
string Dog::getDogName()
{
    return dogName;
}

暫無
暫無

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

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