user1493382
user1493382

Reputation:

VS2010 C++ math support (sin identifier not found)

I have a Windows Form on a new C++ project, a Button1, and inside the Button1 code am using some trig functions. I also have #include <cmath> in the resource.h file next to Form1.h file. (Below is the contents of resource.h):

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by app.rc
#include <cmath>

Why is the code not seeing the trig function?

The Button1 code is as follows:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

                 double x[1000];
                 double y[1000];
                 double hifac;
                 double px[1000];
                 double py[1000];
                 int nout, jmax;
                 double prob;
                 int i,period;
                 period=300;                                 
                 for (i=0; i<1000;i++){
                     x[i]=i;
                     y[i]=sin(2 * 3.14 * i / period);
                 }

             }

Upvotes: 2

Views: 1880

Answers (1)

Hans Passant
Hans Passant

Reputation: 942080

Just put appropriate code in its appropriate place. That #include does not belong in resource.h, that header file should be reserved strictly for resource identifier definitions for unmanaged resources. Just about anywhere else is appropriate, obvious choices are source file where this code appears and the stdafx.h precompiled header file.

And use the appropriate math function. It is std::sin(), the missing namespace name is surely why you get the compiler complaint. But calling that function from managed code is inefficient, extra work is needed to run native code from managed code. It isn't much extra work but it is unnecessary work. Use Math::Sin() instead.

Upvotes: 2

Related Questions