I just wanted to modify the example from zeroc a little ( the one with printer). So here's how it goes.

Here is the little interface in Pritner.ice file:

#pragma once

module Demo
{

    interface Printer
    {
    void printString(string s);
    void printFloat(float val);
    };

};

and the class with server side which produces errors.

#include <Ice/Ice.h>
#include <Printer.h>

using namespace std;
using namespace Demo;

class PrinterI : public Printer
{
public:

   virtual void printString(const string &, const Ice::Current&);
    virtual void printFloat(const float &, const Ice::Current&);    
};


void 
PrinterI::
printFloat(const float &val,const Ice::Current&)
{
    cout<<val<<endl;
}

void 
PrinterI::
printString(const string &s, const Ice::Current&)
{
    cout << s << endl;
}

error looks like this:

Server.cpp: In function ‘int main(int, char**)’:
Server.cpp:49:37: error: cannot allocate an object of abstract type ‘PrinterI’
         Ice::ObjectPtr object = new PrinterI;
                                     ^
Server.cpp:16:7: note:   because the following virtual functions are pure within ‘PrinterI’:
 class PrinterI : public Printer
       ^
In file included from Server.cpp:11:0:
./Printer.h:430:18: note:   virtual void Demo::Printer::printFloat(Ice::Float, const Ice::Current&)
     virtual void printFloat(::Ice::Float, const ::Ice::Current& = ::Ice::Current()) = 0;
                  ^
make: *** [Server.o] Error 1

code in main:

int
main(int argc, char* argv[]){
int status = 0;
Ice::CommunicatorPtr ic;
try
{
    ic = Ice::initialize(argc, argv);
    Ice::ObjectAdapterPtr adapter =
        ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -h localhost -p 10000");
    Ice::ObjectPtr object = new PrinterI;
    adapter->add(object, ic->stringToIdentity("SimplePrinter"));
    adapter->activate();
    ic->waitForShutdown();
}
catch(const Ice::Exception& e)
{
    cerr << e << endl;
    status = 1;
}
catch(const char* msg)
{
    cerr << msg << endl;
    status = 1;
}
if(ic)
{
    try
    {
        ic->destroy();
    }
    catch(const Ice::Exception& e)
    {
        cerr << e << endl;
        status = 1;
    }
}
return status;
}

The part for printString is fine and works well but when i want to have float as argument in intreface's function, then it produces error. I admit i am no pro in c++ but i've just ran out of any clues on how to fix this.

有帮助吗?

解决方案

ZeroC Ice defines its own type for floating point parameters, Ice::Float. Therefore using plain float does not do the job and the original method stays undefined.

Moreover, apart from using the Ice::Float type, the argument is passed by value, not as a reference to constant, therefore you should remove the other parts in the argument declaration like const and & as well.

After all, the error message says it all - the method signature should look exactly like this:

void printFloat(Ice::Float, const Ice::Current&)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top