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 1094421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:58:14+00:00 2026-05-16T23:58:14+00:00

I have working C++ code using swig which creates a struct, passes it to

  • 0

I have working C++ code using swig which creates a struct, passes it to lua (essentially by reference), and allows manipulation of the struct such that the changes made in the lua code remain once I’ve returned to the C++ function. This all works fine until I add a std::string to the struct, as shown here:

struct stuff
{
    int x;
    int y;
    std::string z;
};

I’m unable to modify the std::string because it’s apparently passed as a const reference. If I attempt to assign a value to this string in my lua function I get this error:

Error in str (arg 2), expected ‘std::string const &’ got ‘string’

What is the proper way to address this problem? Do I have to write some custom C++ function to set z rather than using normal syntax like obj.z = "hi"? Is there some way to allow this assignment using swig?

The C++ code is


#include <stdio.h>
#include <string.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include "example_wrap.hxx"

extern int luaopen_example(lua_State* L); // declare the wrapped module

int main()
{

    char buff[256];
    const char *cmdstr = "print(33)\n";
    int error;
    lua_State *L = lua_open();
    luaL_openlibs(L);
    luaopen_example(L);

    struct stuff b;

    b.x = 1;
    b.y = 2;

    SWIG_NewPointerObj(L, &b, SWIGTYPE_p_stuff, 0);
    lua_setglobal(L, "b");

     while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                lua_pcall(L, 0, 0, 0);
        if (error) {
          fprintf(stderr, "%s", lua_tostring(L, -1));
          lua_pop(L, 1);  /* pop error message from the stack */
        }
      }

      printf("B.y now %d\n", b.y);
      printf("Str now %s\n", b.str.c_str());
      luaL_dostring(L, cmdstr);
      lua_close(L);
      return 0;

}
  • 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-05-16T23:58:14+00:00Added an answer on May 16, 2026 at 11:58 pm

    You need to add %include <std_string.i> to your SWIG module. Otherwise, it does not know how to map a Lua string to an C++ std::string.


    A common problem that people encounter is that of classes/structures containing a std::string. This can be overcome by defining a typemap. For example:

    %module example
    %include "std_string.i"
    
    %apply const std::string& {std::string* foo};
    
    struct my_struct
    {
      std::string foo;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is a client issue. It warns you that the… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer Unless the app itself has file sharing built in, you… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer I found something here that does just what I want… May 17, 2026 at 2:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a working code which creates a window with a text area. The
I have working code using the spring-ws library to respond to soap requests. I
So I have working code that animates a BG image via a plugin. This
I have some working code that uses IImgCtx to load images, but I can't
I having an issue from my HTTP Post. The code I'm using are working
I am working on a project where we have many JSF pages - using
I have this project i'm working on and id like to add a really
I have been working on quite a complex accordion/form recently and have a select
I'm working on a C++ project with a large number of classes (150+), each
I'm trying to use python to sftp a file, and the code works great

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.