Exception CORBA接口中的属性-在服务器编译后出现错误
又是我和科尔巴有麻烦了。我只是想在CORBA中编写一些示例接口,其中接口将具有一个属性 这是我的idl文件:Exception CORBA接口中的属性-在服务器编译后出现错误,exception,corba,Exception,Corba,又是我和科尔巴有麻烦了。我只是想在CORBA中编写一些示例接口,其中接口将具有一个属性 这是我的idl文件: interface Interfface { readonly attribute double number; exception myOwnException { string reason; }; void ffunction(in double arg) raises (myOwnException); double g
interface Interfface
{
readonly attribute double number;
exception myOwnException {
string reason;
};
void ffunction(in double arg) raises (myOwnException);
double getNumber();
void setNumber(in double number);
};
我的IDL接口实现:
#include "interface.hh"
class Implementation : public POA_Interfface
{
private :
double number;
public :
virtual void ffunction(double arg);
virtual double getNumber();
virtual void setNumber(double number);
};
#include "implementation.h"
void Implementation::ffunction(double arg)
{
this->number = 0;
arg++;
throw Interfface::myOwnException("Sth went terribly wrong!");
}
void Implementation::setNumber(double number){
this->number = number;
}
double Implementation::getNumber(){
return this->number;
}
当我编译interface.idl、implementation.h、implementation.cpp时,一切正常。问题是当我想编译我的server.cpp时:
#include "implementation.h"
#include <iostream>
#include <omniORB4/CORBA.h>
#include <omniORB4/Naming.hh>
using namespace std;
int main(int argc, char ** argv)
{
try {
// init ORB
CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);
// init POA
CORBA::Object_var poa_obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(poa_obj);
PortableServer::POAManager_var manager = poa->the_POAManager();
// create service
Implementation * service = new Implementation;
// register within the naming service
try {
CORBA::Object_var ns_obj = orb->resolve_initial_references("NameService");
if (!CORBA::is_nil(ns_obj)) {
CosNaming::NamingContext_ptr nc = CosNaming::NamingContext::_narrow(ns_obj);
CosNaming::Name name;
name.length(1);
name[0].id = CORBA::string_dup("TestServer");
name[0].kind = CORBA::string_dup("");
nc->rebind(name, service->_this());
cout << "Server is running ..." << endl;
}
} catch (CosNaming::NamingContext::NotFound &) {
cerr << "not found" << endl;
} catch (CosNaming::NamingContext::InvalidName &) {
cerr << "invalid name" << endl;
} catch (CosNaming::NamingContext::CannotProceed &) {
cerr << "cannot proceed" << endl;
}
// run
manager->activate();
orb->run();
// clean up
delete service;
// quit
orb->destroy();
} catch (CORBA::UNKNOWN) {
cerr << "unknown exception" << endl;
} catch (CORBA::SystemException &) {
cerr << "system exception" << endl;
}
}
#包括“implementation.h”
#包括
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
试一试{
//初始球
CORBA::ORB_ptr ORB=CORBA::ORB_init(argc,argv);
//初始POA
CORBA::Object_var poa_obj=orb->resolve_initial_references(“RootPOA”);
PortableServer::POA_var POA=PortableServer::POA::_狭窄(POA_obj);
PortableServer::POAManager_var manager=poa->the_POAManager();
//创建服务
实现*服务=新实现;
//在命名服务中注册
试一试{
CORBA::Object\u var ns\u obj=orb->解析初始引用(“名称服务”);
如果(!CORBA::is_nil(ns_obj)){
CosNaming::NamingContext_ptr nc=CosNaming::NamingContext::_狭窄(ns_obj);
CosNaming::Name;
名称、长度(1);
名称[0]。id=CORBA::string_dup(“TestServer”);
名称[0]。种类=CORBA::字符串_dup(“”);
nc->重新绑定(名称、服务->_this());
cout通过声明一个属性号,您将自动获得一个number()访问器和设置,这两个访问器和设置都必须在您的仆人中实现。您不需要在IDL中定义getNumber()和setNumber(),只需从属性中删除readonly标记即可同时拥有setter
只需将该属性视为自动生成可远程访问的setter和getter的属性。谢谢您的回答。我将“number”定义为只读属性,因为我希望它在我的接口中是私有的(接口中的私有字段号)。是否可能?正如您所写,我删除了“readonly”声明,但仍有错误:通过在IDL中添加属性,可以远程访问该属性。通过删除只读,您还必须实现setter。如果您想让它们完全私有,请从IDL中删除属性,并将它们作为类成员添加到实现中。