# Problem in using typedef with c++

**URL:** <https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352>\
**Category:** Ruby\
**Created:** [March 19, 2008, 11:24am UTC](https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352 "2008-03-19T11:24:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonyaim83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tonyaim83](https://www.ruby-forum.com/u/tonyaim83)\
**Post date:** [March 19, 2008, 11:24am UTC](https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352/1 "2008-03-19T11:24:55Z")

</div>

Hi  
My code is as follows :-  
example.h

#include"iostream.h"  
using namespace std;  
typedef int MYINT;  
void sum(MYINT a, MYINT b);

example.cpp  
#include"example.h"  
void sum(MYINT a, MYINT b)  
{  
MYINT c;  
c=a +b;  
cout\<\<c;  
}

example.i  
%module example  
%{  
#include"example.h"  
extern void sum(MYINT a, MYINT b);  
%}

#include “example.h”  
extern void sum(MYINT a,MYINT b);

example.rb

require “example.so”  
MYINT a=10;  
MYINT b=20;  
Example.sum(a,b);

But it is giving me error undefined MYINT when i’m trying to compile

> this  
> Kindly help

---

<div class="post-metadata">

**Author:** ![tonyaim83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tonyaim83](https://www.ruby-forum.com/u/tonyaim83)\
**Post date:** [March 19, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352/2 "2008-03-19T13:17:42Z")

</div>

Hi,

At Wed, 19 Mar 2008 19:24:46 +0900,  
Abhi Us wrote in [ruby-talk:295033]:

> example.rb
> 
> require “example.so”  
> MYINT a=10;  
> MYINT b=20;

Is this file written in Ruby, or in C++?

---

<div class="post-metadata">

**Author:** ![tonyaim83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tonyaim83](https://www.ruby-forum.com/u/tonyaim83)\
**Post date:** [March 19, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352/3 "2008-03-19T14:12:19Z")

</div>

On Wed, Mar 19, 2008 at 07:24:46PM +0900, Abhi Us wrote:

> Hi  
> My code is as follows :-  
> example.h
> 
> #include"iostream.h"  
> using namespace std;

Please don’t put “using namespace” inside header files. This can cause  
conflict with code that includes the header but doesn’t expect to get  
the namespace.

> }
> 
> example.rb
> 
> require “example.so”  
> MYINT a=10;  
> MYINT b=20;  
> Example.sum(a,b);
> 
> But it is giving me error undefined MYINT when i’m trying to compile

In Ruby, variables do not have types (all variables are references to  
Objects, and the type of the Object is determined dynamicaly). If you  
write instead:

a=10  
b=20  
Example.sum(a,b)

I think it should work.

Note that you don’t need to terminate your lines with a semicolon in  
Ruby.

Paul

---

<div class="post-metadata">

**Author:** ![tonyaim83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tonyaim83](https://www.ruby-forum.com/u/tonyaim83)\
**Post date:** [March 24, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352/4 "2008-03-24T16:10:50Z")

</div>

On Thu, Mar 20, 2008 at 02:20:17PM +0900, Abhi Us wrote:

> Hi i got ur point that ruby do not have types. Then suppose if i have  
> user defined data type for e.g
> 
> map\<int,string\> MYmap;  
> is present in C++ code.How should i create the instances of it in ruby.  
> Can i write like  
> m1.insert(1,“amey”)  
> and it will be treated as the map which i have defined…

You probably want to create a typemap between your map type and ruby’s  
hash type. You can read about typemaps here:

[http://www.swig.org/Doc1.3/Typemaps.html](http://www.swig.org/Doc1.3/Typemaps.html)

Paul

---

<div class="post-metadata">

**Author:** ![tonyaim83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tonyaim83](https://www.ruby-forum.com/u/tonyaim83)\
**Post date:** [March 20, 2008, 6:20am UTC](https://www.ruby-forum.com/t/problem-in-using-typedef-with-c/132352/5 "2008-03-20T06:20:25Z")

</div>

Paul B. wrote:

> On Wed, Mar 19, 2008 at 07:24:46PM +0900, Abhi Us wrote:
> 
> > Hi  
> > My code is as follows :-  
> > example.h
> > 
> > #include"iostream.h"  
> > using namespace std;
> 
> Please don’t put “using namespace” inside header files. This can cause  
> conflict with code that includes the header but doesn’t expect to get  
> the namespace.
> 
> > }
> > 
> > example.rb
> > 
> > require “example.so”  
> > MYINT a=10;  
> > MYINT b=20;  
> > Example.sum(a,b);
> > 
> > But it is giving me error undefined MYINT when i’m trying to compile
> 
> In Ruby, variables do not have types (all variables are references to  
> Objects, and the type of the Object is determined dynamicaly). If you  
> write instead:
> 
> a=10  
> b=20  
> Example.sum(a,b)
> 
> I think it should work.
> 
> Note that you don’t need to terminate your lines with a semicolon in  
> Ruby.
> 
> Paul

Hi i got ur point that ruby do not have types. Then suppose if i have  
user defined data type for e.g

map\<int,string\> MYmap;  
is present in C++ code.How should i create the instances of it in ruby.  
Can i write like  
m1.insert(1,“amey”)  
and it will be treated as the map which i have defined…
