2
在C++中,我有:代表在C + +(或這樣的事情)
//Base1.h
#ifndef BASE1_H
#define BASE1_H
#include <iostream>
#include <string>
#include "Base2.h"
using namespace std;
class Base1{
private:
string name;
public:
Base1(string _name);
void printSomething();
string getName();
};
#endif
在Base1.cpp
我實現構造Base1(string _name)
和string getName()
爲正常,並且printSomething()
:
void Base1::printSomething(){
Base2 b2;
// how to pass a parameter in the following code?
b2.printBase1();
}
// This is Base2.h
#ifndef BASE2_H
#define BASE2_H
#include <iostream>
#include <string>
#include "Base1.h"
using namespace std;
class Base2{
public:
Base2();
void printBase1(Base1 b);
};
#endif
而且Base2()
構造我像往常一樣執行,這是我的printBase1(Base1 b)
:
void Base2::printBase1(Base1 b){
cout << b.getName();
}
所以,最後,我想在Base1
類中使用printSomething()
,但我不知道如何將參數傳遞給b2.printBase1()
的printSomething()
,如上所示在我的代碼中。在C++中有沒有像b2.printBase1(this)
?如果不是,你能給我一個建議嗎?
如果我刪除'Base2.h',如何在printSomething()中創建'Base2 b2''?我得到了一個錯誤:(! –
@Kingfisher你把它包含在cpp文件中,而不是頭文件 –
謝謝,但我不會改變'void printBase1(Base1 b)',我仍然得到一個正確的結果! ? –