![](/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.