在我的CPP文件C++中从类头中访问私有成员的问题 晨曦 Morg.cpp
我在尝试获取和设置名称或其他任何内容时会出错,因为它表示名称未定义,我认为应该使用标题来避免这种情况,所以我想知道我哪里出错了在我的CPP文件C++中从类头中访问私有成员的问题 晨曦 Morg.cpp,c++,class,undefined,C++,Class,Undefined,我在尝试获取和设置名称或其他任何内容时会出错,因为它表示名称未定义,我认为应该使用标题来避免这种情况,所以我想知道我哪里出错了 顺便说一句:我正在使用MS Visual Studio 2012。您的.h不安全。这是一个有守卫的版本,包括适当的,感谢丹尼尔指出这一点。这是一个很好的行为,保持监护人,并包括你的声明中使用的图书馆 #IFNDEF __MORG_H #DEFINE __MORG_H #include <string> //-------
顺便说一句:我正在使用MS Visual Studio 2012。您的.h不安全。这是一个有守卫的版本,包括适当的,感谢丹尼尔指出这一点。这是一个很好的行为,保持监护人,并包括你的声明中使用的图书馆
#IFNDEF __MORG_H
#DEFINE __MORG_H
#include <string>
//-----------Morg-----------//
class Morg{
public:
void setName( std::string morgName );
void setType( char morgType );
void setXcoord( int xLocation );
void setYcoord( int yLocation );
void moveMorg( int currDirection );
void consume( Morg morg );
void reproduce( char morgType );
void setBounds( int upperBound );
void setDirection( int currDirection );
char getType();
private:
std::string Name;
char Type;
int yCoord;
int xCoord;
int bounds;
int direction;
};
#ENDIF
那就应该成功了 函数定义的名称应为
void Morg::setName( std::string morgName ){ Name = morgName; }
std::string Morg::getName(){return Name;}
void Morg::setType( char morgType ){}
void Morg::setXcoord( int xLocation ){}
void Morg::setYcoord( int yLocation ){}
void Morg::moveMorg( int currDirection ){}
等等等等
这是Morg类的函数部分,而不是顶级函数,因为如果没有名称限定,它们将是顶级函数。include masterIncludes.h//这有一堆std内容,如list和iostream等。-这可能没有你想象的那么好。我猜这一定是Morg::SetName你需要一本更好的书来学习。这对我来说不起作用,我很感激给警卫的建议,我只是还没有真正做到这一点,因为我只是想让基本功能发挥作用。我仍然无法理解我指的是哪个名字。请给我你的编译器的完整信息@初学者binxthank you,这解决了我的问题。我知道这很简单,只是因为某种原因我在谷歌上搜索时找不到它。
#IFNDEF __MORG_H
#DEFINE __MORG_H
#include <string>
//-----------Morg-----------//
class Morg{
public:
void setName( std::string morgName );
void setType( char morgType );
void setXcoord( int xLocation );
void setYcoord( int yLocation );
void moveMorg( int currDirection );
void consume( Morg morg );
void reproduce( char morgType );
void setBounds( int upperBound );
void setDirection( int currDirection );
char getType();
private:
std::string Name;
char Type;
int yCoord;
int xCoord;
int bounds;
int direction;
};
#ENDIF
// #include "masterIncludes.h" //this is bad habit
#include "Morg.h"
void Morg::setName( std::string morgName ){
Name = morgName; //I get an error here it thinks name is undefined I've tried Morg::Name and it stays undefined I dont know what im supposed to do.
}
std::string getName(){
return Name;
}
void Morg::setType( char morgType ){
}
void Morg::setXcoord( int xLocation ){
}
void Morg::setYcoord( int yLocation ){
}
void Morg::moveMorg( int currDirection ){
}
void Morg::consume( Morg morg ){
}
void Morg::reproduce( char morgType ){
}
void Morg::setBounds( int upperBound ){
}
void Morg::setDirection( int currDirection ){
}
char Morg::getType(){
}
void Morg::setName( std::string morgName ){ Name = morgName; }
std::string Morg::getName(){return Name;}
void Morg::setType( char morgType ){}
void Morg::setXcoord( int xLocation ){}
void Morg::setYcoord( int yLocation ){}
void Morg::moveMorg( int currDirection ){}