![](/img/trans.png)
[英]C++, Is it possible / how would you define a class constructor that calls a function in the .h & .cpp source files
[英]In C++ how do you define ***.h for class?
我有 3 个文件:***.cpp *2 和 ***.h *1
一旦我输入终端: g++ mymain.cpp landowner.cpp -o mymain 它说
错误:此 scope 8 中未声明“土地所有者” 地主亚历克斯; | ^~~~~~~~~
//landowner.h
#ifndef LANDOWNER
#define LANDOWNER
using namespace std;
void ShowScore();
#endif
//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;
}
这非常令人困惑。 我肯定错过了什么。 有人能帮我吗? 谢谢!
class 声明应放在 header 内。
//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
使用using namespace std;
(特别是在标题中)是坏的,所以应该删除它并更改代码的 rest 部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.