C++ 在C++;你如何为班级定义***.h?
我有3个文件:**.cpp*2和***.h*1 一旦我进入终端:g++mymain.cpp landowner.cpp-o mymain 上面说 错误:“土地所有者”未在此范围内声明 8 |土地所有者Alex; |^~~~~~~~~C++ 在C++;你如何为班级定义***.h?,c++,C++,我有3个文件:**.cpp*2和***.h*1 一旦我进入终端:g++mymain.cpp landowner.cpp-o mymain 上面说 错误:“土地所有者”未在此范围内声明 8 |土地所有者Alex; |^~~~~~~~~ //landowner.cpp #包括“landowner.h” #包括 土地所有者类别 { 私人: 字符串名; 长分数; 智能卡[20]; 公众: 土地所有者(){} ~landowner(){} void ShowScore(){ cout分类应放在标题内 //
//landowner.cpp
#包括“landowner.h”
#包括
土地所有者类别
{
私人:
字符串名;
长分数;
智能卡[20];
公众:
土地所有者(){}
~landowner(){}
void ShowScore(){
cout分类应放在标题内
//landowner.h
#ifndef LANDOWNER
#define LANDOWNER
#include <iostream>
#include <string>
using namespace std;
void ShowScore();
class landowner
{
private:
string name;
long score;
int cards[20];
public:
landowner(){}
~landowner(){}
void ShowScore(){
cout<<name<<"current score:"<<score<<endl;
}
protected:
};
#endif
使用名称空间std;
(尤其是在标题中)是不好的,因此应该删除它,并更改代码的其余部分
嗨,谢谢你的回答。这对我帮助很大!!!
//landowner.cpp
#include "landowner.h"
#include <iostream>
class landowner
{
private:
string name;
long score;
int cards[20];
public:
landowner(){}
~landowner(){}
void ShowScore(){
cout<<name<<"current score:"<<score<<endl;
}
protected:
};
//mymain.cpp
#include <iostream>
#include "landowner.h"
using namespace std;
int main(){
landowner Alex;
Alex.name = Alex;
Alex.score = 100;
Alex.ShowScore();
return 0;
}
//landowner.h
#ifndef LANDOWNER
#define LANDOWNER
#include <iostream>
#include <string>
using namespace std;
void ShowScore();
class landowner
{
private:
string name;
long score;
int cards[20];
public:
landowner(){}
~landowner(){}
void ShowScore(){
cout<<name<<"current score:"<<score<<endl;
}
protected:
};
#endif
//landowner.cpp
#include "landowner.h"
// currently we have nothing to implement here