0

又是我在使用 CORBA 时遇到了麻烦。我只是想在 CORBA 中编写一些示例接口,其中接口将具有一个属性。

这是我的idl文件:

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;
    }
} 

它给了我错误:

server.cpp:在函数'int main(int,char**)'中:server.cpp:20:错误:无法分配抽象类型'Implementation' implementation.h:4的对象:注意:因为以下虚拟函数是纯在“实现”中:interface.hh:197:注意:虚拟 CORBA::Double _impl_Interfface::number()

似乎 CORBA 将我的“数字”属性视为一个函数,而不是一个属性,对吗?如何解决?

4

1 回答 1

0

通过声明一个属性 number,您将自动获得一个 number() 访问器和设置,您都必须在您的仆人中实现它们。您不需要在 IDL 中定义 getNumber() 和 setNumber(),只需从属性中删除 readonly 标记即可拥有 setter。

只需将属性视为自动生成可远程访问的 setter 和 getter 的东西。

于 2012-06-25T14:03:48.173 回答