我有以下問題:我正在使用創建名爲server_t的對象的現有類。 另一個函數期望* server_t作爲參數。如何在C++類中使用引用和指針?
我想縮小代碼,並加入具有以下成員的類:
#ifndef _PMCLASS
#define _PMCLASS
#include "pmlib.h"
class pmServer{
private:
server_t server ;
counter_t counter;
line_t lines;
server_t * server2;
int set, frequency, aggregate ;
public:
pmServer();
pmServer(int set, int frequency, int aggregate);
~pmServer();
void setSet(int s);
void setFrequency(int f);
void setAggregate(int a);
int getSet(void);
int getFrequency(void);
int getAggregate(void);
server_t* getServerT(void);
counter_t* getCounterT(void);
line_t* getLineT(void);
server_t* getZeiger(void);
};
#endif
然後我創建的構造函數:
#include "pmClass.h"
#include <iostream>
using namespace std;
void pmServer::setSet(int s){
this->set = s;
}
void pmServer::setFrequency(int f){
this->frequency = f;
}
void pmServer::setAggregate(int a){
this->aggregate = a;
}
int pmServer::getSet(void){
return set;
}
int pmServer::getFrequency(void){
return frequency;
}
int pmServer::getAggregate(void){
return aggregate;
}
server_t* pmServer::getPointer(){
return &server;
}
pmServer::pmServer(){
set = -1;
frequency = 0;
aggregate = 1;
}
然後我試圖創建一個對象 - >工作,但後來我想使用pm_set_server(...) 它想要下面的參數:int pm_set_server(char * ip,int port,server_t * pm_server)
void run() {
build_initial_mesh();
// Construct/read in the initial mesh.
pmServer server1;
pm_set_server("xxx.xxx.xxx.xxx", 6526,server1.getPointer); //its a correct ip address , no panic :)
我得到的:
錯誤:類型的參數 'server_t *'(pmServer ::)()不匹配 'server_t *'
但這個工作沒有任何問題:
void run() {
// Construct/read in the initial mesh.
//pmServer server1;
server_t test;
pm_set_server("xxx.xxx.xxx.xxx", 6526,&test);
build_initial_mesh();
事情是,我不想創建每一個新的ojects,並想在構造函數中做到這一點...有人有什麼想法嗎?
謝謝。
問候托馬斯
'server'和'server2'有什麼意義?他們似乎沒有被使用。 – Ashalynd 2014-09-12 22:24:43