Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:12:31+00:00 2026-06-15T13:12:31+00:00

While trying to do a simple call by reference from python into a C++

  • 0

While trying to do a simple call by reference from python into a C++ class method:

class Foo {
  protected:
    int _internalVal;
  public:
    Foo() : _internalVal(5){}
    void getVal(int& val_io) {val_io = _internalVal;}
    void getValDoesNothing(int val_io) {val_io = _internalVal;}
}

It is possible to compile the boost wrapper code:

BOOST_PYTHON_MODULE(libBar) {
  boost::python::class_<Foo>("Foo")
    .def("getVal", &Foo::getVal)
    .def("getValDoesNothing", &Foo::getValDoesNothing);
}

However when executing the functionality in python an error occurs:

In [1]: import libBar

In [2]: f = libBar.Foo()

In [3]: f
Out[3]: <libBar.Foo at 0x2b483c0>

In [4]: val = int()

In [5]: #next command is just to check function signature type

In [6]: f.getValDoesNothing(val)

In [7]: f.getVal(val)
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-5-531e4cea97c2> in <module>()
----> 1 f.getVal(val)

ArgumentError: Python argument types in
    Foo.getVal(Foo, int)
did not match C++ signature:
    getVal(Foo {lvalue}, int {lvalue})

I’m working with a C++ library I don’t control so changing getVal to return the value isn’t an option.

Is there any way to make the last Python command work?

I’ll even take a fix that doesn’t change the Python variable but still allows the function call.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:12:32+00:00Added an answer on June 15, 2026 at 1:12 pm

    What you are trying to achieve is not valid in Python. Integers are immutable, so you can’t simple call a function and hope it’s going to change its content.

    Since you are working with a library you don’t control and changing getVal to return the value isn’t an option, you can create an wrapper like that:

    int getVal(Foo &foo) {
        int val_io;
        foo.getVal(val_io);
        return val_io;
    };
    
    BOOST_PYTHON_MODULE(libBar) {
        boost::python::def("getVal", getVal);
        ...
    }
    

    and then use in this way:

    In [1]: import libBar
    
    In [2]: f = libBar.Foo()
    
    In [3]: f
    Out[3]: <libBar.Foo at 0x2b483c0
    
    In [3]: libBar.getVal(f)
    Out[3]: 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a simple Java method call from C++ while Java calls
I am trying to make a simple call to the pow() function from math.h
When trying to call a web method from the .net compact framework I get
I am trying to call MSTest.exe from a simple console app that is executed
I am getting strange errors while trying to create a simple database using isql
While i am trying to get value from arraylist,i got the following errors.My sample
While trying to search with id sorting (and paginating) im getting from the log:
I'm trying to expose a fairly simple C# class to COM which should be
I'm trying to make a simple ProgressDialog appear while my AsyncTask is fetching data.
I am facing a class resolution issue while trying to make my architecture flexible.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.