C++ c++;getter未返回值(NULL)

C++ c++;getter未返回值(NULL),c++,return,console-application,return-value,getter,C++,Return,Console Application,Return Value,Getter,当我尝试coutget函数时,它不会返回任何值 我的头文件: #pragma once #include "stdafx.h" #include <iostream> #include <string> using namespace std; class Dog { private: string dogName = "Max"; public: Dog(); void talking(); void jumping(); v

当我尝试
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();
};
#pragma一次
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
班犬
{
私人:
字符串dogName=“Max”;
公众:
狗();
避免说话;
空跳();
void setDogName(字符串newDogName);
///////这就是功能/////
字符串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;
}
#包括“stdafx.h”
#包括
#包括
#包括“Dog.h”
使用名称空间std;
狗{

cout问题出在函数
void Dog::setDogName(string newDogName)
newDogName=dogName;
行中。您使用的
赋值运算符不正确(
=
),这应该是
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;
}
#包括“stdafx.h”
#包括
#包括
#包括“Dog.h”
使用名称空间std;
狗{

cout问题出在函数
void Dog::setDogName(string newDogName)
newDogName=dogName;
行中。您使用的
赋值运算符不正确(
=
),这应该是
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;
}
#包括“stdafx.h”
#包括
#包括
#包括“Dog.h”
使用名称空间std;
狗{

如果您在
setDogName
方法中执行以下操作:
dogName=newDogName;
如果您通过
const string&
传递字符串,您将在
setDogName
函数中看到错误,因为编译器不允许分配给
newDogName
。换句话说
void Dog::setDogName>(常量字符串和newDogName)
。哦,老兄!太感谢你了!!!!我什么都试过了,但没有试过!我真是太笨了!!谢谢你在
setDogName
方法中这样做:
dogName=newDogName;
如果你通过
const string&
传递字符串,你就会在
setDogName
函数中看到你的错误,因为编译器不会我允许分配给
newDogName
。换句话说
void Dog::setDogName(const string&newDogName)
。哦,伙计!非常感谢!!!!我什么都试过了,但没有试过!我真是太棒了!!谢谢你