Basic C++ Question

View: New views
2 Messages — Rating Filter:   Alert me  

Parent Message unknown Basic C++ Question

by aasmith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to use SWIG on some basic C++ files.  My code follows at the
end of this note.  Basically, SWIG seems to work just fine, until I try to
compile the resulting wrapper.cxx file.  When I do that, I get a ton of
errors.  They look like scoping errors, but the wrapper code is
complicated enough that I'm really not sure.

$ swig -python -c++ Stupid.i
$ g++ -c -fpic Stupid_wrap.cxx -I/usr/include/python2.6/
Stupid_wrap.cxx: In function PyObject* _wrap_new_Stupid(PyObject*, PyObject*):
Stupid_wrap.cxx:2765: error: Stupid was not declared in this scope
Stupid_wrap.cxx:2765: error: result was not declared in this scope
Stupid_wrap.cxx:2773: error: expected primary-expression before ) token
Stupid_wrap.cxx:2773: error: expected `;' before new

(and many more)

This happens regardless of which language I try to use (Python, Perl,
Java, etc.).  It's also happened for other, more complicated files.  I've
been banging my head against my monitor on this one for a few days--any
help would be welcome.

Thanks,
Adam

Stupid.cpp:

#include "Stupid.h"

using namespace std;

Stupid::Stupid(int n) {
   number = new int(1);
   *number = n;
}

Stupid::~Stupid(void) {
   delete number;
}

int Stupid::getNumber(void) {
   return *number;
}

void Stupid::setNumber(int n) {
   *number = n;
}

void Stupid::printNumber(void) {
   cout << *number << "\n";
}


Stupid.h:

#include <iostream>

#ifndef STUPID_DEFINED
#define STUPID_DEFINED

class Stupid {
  private:
   int *number;
  public:
   Stupid(int n);
   ~Stupid(void);
   int getNumber(void);
   void setNumber(int n);
   void printNumber(void);
};

#endif

Stupid.i:

%module stupid
%{
#include <iostream>
%}

class Stupid {
   private:
     int *number;
   public:
     Stupid(int n);
     ~Stupid(void);
     int getNumber(void);
     void setNumber(int n);
     void printNumber(void);
};


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Basic C++ Question

by David Beazley-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You're missing a header file in the %{ ... %} block.  You probably need

%{
#include "stupid.h"
%}

and it will all work.

Cheers,
Dave

On Jun 24, 2009, at 5:13 AM, Adam A Smith wrote:

> I'm trying to use SWIG on some basic C++ files.  My code follows at  
> the
> end of this note.  Basically, SWIG seems to work just fine, until I  
> try to
> compile the resulting wrapper.cxx file.  When I do that, I get a ton  
> of
> errors.  They look like scoping errors, but the wrapper code is
> complicated enough that I'm really not sure.
>
> $ swig -python -c++ Stupid.i
> $ g++ -c -fpic Stupid_wrap.cxx -I/usr/include/python2.6/
> Stupid_wrap.cxx: In function PyObject* _wrap_new_Stupid(PyObject*,  
> PyObject*):
> Stupid_wrap.cxx:2765: error: Stupid was not declared in this scope
> Stupid_wrap.cxx:2765: error: result was not declared in this scope
> Stupid_wrap.cxx:2773: error: expected primary-expression before )  
> token
> Stupid_wrap.cxx:2773: error: expected `;' before new
>
> (and many more)
>
> This happens regardless of which language I try to use (Python, Perl,
> Java, etc.).  It's also happened for other, more complicated files.  
> I've
> been banging my head against my monitor on this one for a few days--
> any
> help would be welcome.
>
> Thanks,
> Adam
>
> Stupid.cpp:
>
> #include "Stupid.h"
>
> using namespace std;
>
> Stupid::Stupid(int n) {
>   number = new int(1);
>   *number = n;
> }
>
> Stupid::~Stupid(void) {
>   delete number;
> }
>
> int Stupid::getNumber(void) {
>   return *number;
> }
>
> void Stupid::setNumber(int n) {
>   *number = n;
> }
>
> void Stupid::printNumber(void) {
>   cout << *number << "\n";
> }
>
>
> Stupid.h:
>
> #include <iostream>
>
> #ifndef STUPID_DEFINED
> #define STUPID_DEFINED
>
> class Stupid {
>  private:
>   int *number;
>  public:
>   Stupid(int n);
>   ~Stupid(void);
>   int getNumber(void);
>   void setNumber(int n);
>   void printNumber(void);
> };
>
> #endif
>
> Stupid.i:
>
> %module stupid
> %{
> #include <iostream>
> %}
>
> class Stupid {
>   private:
>     int *number;
>   public:
>     Stupid(int n);
>     ~Stupid(void);
>     int getNumber(void);
>     void setNumber(int n);
>     void printNumber(void);
> };
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> Swig-user@...
> https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user