Error "C3145" and "C2061" in C++ Visual Studio - visual-studio-2010

EDIT: What is C++/CLI? I am programming in Visual studio, and as far as I know using C++... Also, The first error was solved by Peter's comment, but I am still stuck on the second.
I am brand new to the world of C++, and have previously done all my work in Java. I am unfamiliar with the use of pointers and garbage collection (though I believe I understand the concept) and I believe that may be the source of my problems. I am getting the following error messages:
1>Runner.cpp(6): error C3145: 'formOutOfTime' : global or static variable may not have managed type 'System::Windows::Forms::Form ^'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>Runner.cpp(22): error C2061: syntax error : identifier 'FormOutOfTime'
My code is like this:
PurpleHealth.cpp (This is the file I believe the system calls to start it all off):
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
#include "Runner.h"
using namespace PurpleHealth;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
//Application::Run(gcnew FormOutOfTime());
Runner* runner = new Runner();
//delete runner;
return 0;
}
Runner.h (this is the header file I want to run all my main code, and launch the forms. I also struggle with the purpose behind the header files)
#include "stdafx.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
class Runner
{
public:
Runner();
~Runner();
// functions
private:
void Go();
// member variables
};
And Finally Runner.cpp:
#include "stdafx.h"
#include "Runner.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
//Variable Dclaration
System::Windows::Forms::Form^ formOutOfTime;//Error Here***************************
Runner::Runner()
{
// Do stuff if you need to
this->Go();
}
Runner::~Runner()
{
// Clear memory if you need to
}
void Runner::Go()
{
formOutOfTime = gcnew FormOutOfTime();//Error Here***************************
formOutOfTime->ShowDialog();
}
Please help me solve these messages, and even critique on form is appreciated. Thanks.

managed pointers cannot be declared at static or global scope. They can only be declared at function scope. Move the declaration of formOutOfTime from the top of the runner.cpp file to within the Go method

Related

How to wrap a c++ class with an opencv object with c++/cli

I would like to wrap a complex c++ class which used opencv object with c++/cli for using in c# and vb. I'm using Visual Studio 2015. I'm using the x64 versions of of the OpenCV library and compiling also to x64.
Following is the header.h file of a small test class, just as an example:
#pragma once
#include <vector>
#include "opencv2/opencv.hpp"
using namespace std;
class compC
{
public:
compC(int * pInt, int arrSize);
int sumArray();
private:
vector<int> vec;
cv::Mat img;
};
This is going to be compiled as a win32-application to a .lib file without any problems.
The example wrapper class (wrapper.h) would look like:
#pragma once
#include "../ConsoleApplication1/header.h"
#include "../ConsoleApplication1/body.cpp"
using namespace System;
namespace WrapperLibrary {
public ref class WrapperClass
{
public:
WrapperClass(int * pInt, int arraySize);
int getSum();
private:
compC * pcC;
int sum;
};
}
The second class would be compiled as a dll with /clr (as c++/cli project).
First I got an error that opencv.hpp could not be find and had to add the include directory of opencv in to the wrapper project. But I cannot compile it and get a lot of errors, mainly pointing to linking problems "unresolved external problems ..." in connection with the opencv-mat object.
If I remove the lines
#include "opencv2/opencv.hpp"
and
cv::Mat img;
in the test class (header.h) all will be fine.
What did I made wrong? How can I wrap those c++ class with c++/cli?

asio socket, inside a class declaration, scope issue

I want to have a class inside which I initialize and open a tcp socket, but I want to repeatedly write to the socket only by calling a method of that class, without having to re-establish connection everytime.
My code is below. I get the compile time errors as pasted here below the code block.
The method is unable to see the socket that was initialized in the constructor. Seems to be a simple C++ "scope" issue, but beats me!
#include <ctime>
#include <iostream>
#include <string>
#include <asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using asio::ip::udp;
using asio::ip::tcp;
class rpcClass {
char sendBuffer[16];
asio::ip::tcp::endpoint epdPt;
asio::io_service io_service;
asio::ip::tcp::endpoint endPt;
public:
rpcClass () { // constructor
asio::ip::tcp::socket socketTCP(io_service);
strcpy (sendBuffer, "*Constructor*\n");
endPt = asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"),\
boost::lexical_cast<int>(5004));
socketTCP.connect(endPt);
socketTCP.write_some (asio::buffer(sendBuffer));
};
void sendRPCData (void) { //send data when called from main()
strcpy (sendBuffer, "rpcData\n");
socketTCP.write_some (asio::buffer(sendBuffer));
};
};
int main(void) {
rpcClass rpc; // I WANT THE rpc OBJECT TO SEND DATA
rpc.sendRPCData (); // ONLY WHEN THIS METHOD IS CALLED
return (0);
}
Here is the error output (Eclipse)
rpc-class.cpp: In member function ‘void rpcClass::sendRPCData()’:
rpc-class.cpp: error: ‘socketTCP’ was not declared in this scope
Thanks a lot for helping!
-pn
You've declared socketTCP as a local variable in the constructor, not a member variable of the class.
That means you can't use it in other functions.
Presumably you meant to declare a member variable, and initialize that in the constructor instead of a local variable.

Error while trying to use strings while working with windows application forms

When creating a new class while working in a project that is based around windows application forms. I have a problem where string becomes unusable. I get errors the say things like
"a member of a managed class cannot be of a non-managed class type"
"IntelliSense: a function type involving a generic parameter cannot have an ellipsis parameter"
"IntelliSense: linkage specification is incompatible with previous "bsearch_s"(declared at line 426)"
Person.h
#pragma once
#include <string>
using namespace std;
ref class Person
{
public:
Person(void);
string name;
};
Person.cpp
#include "stdafx.h"
#include "Person.h"
#include <string>
using namespace std;
Person::Person(void)
{
name = "Bob";
}
If someone has a solution to this or a work around that isn't creating my own string class I would love to hear it as this has been bothering me for days.
String header in CLI C++ is already included in the System namespace.
String works as a pointer. Using the cap ^ handler
//This works:
#include "stdafx.h"
using namespace System;
void function()
{
String^ simpleStr = "Bob";
}

how to fix unsatisfiedLinkError when generating dll for JNI in windows platform using g++

I'm new to native programming. I've been trying to fix the unsatisfiedLinkError past 8-9 hours but got no result. After a lot of googling and stackoverflowing, I got sick of fixing it, I'm posting my problem here. Somebody please please help me.
I'm using g++ compiler in windows 32bit environment.
Here are the files that I've created:
Demo.java
class Demo
{
// Declaration of the native method
public native int methodOfC(int arg1);
/*The native keyword tells the compiler that the implementation of this method is in a native language*/
/*Loading the library containing the implementation of the native method*/
static
{
System.out.println("Control is in Java.......going to call a C program......\n");
System.loadLibrary("try");
System.out.println("Congr8s no prob in CallApi.....\n");
}
public static void main(String[] args)
{
//invoking the native method
int sendToC,getFrmC;
if(args.length!=0) sendToC=Integer.parseInt(args[0]);
else sendToC=999;
Demo ob1=new Demo();
getFrmC=ob1.methodOfC(sendToC);
System.out.println("This is in Java......\n Got "+ getFrmC +" in return from C.");
}//end main
}//end Demo
Demo.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Demo */
#ifndef _Included_Demo
#define _Included_Demo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Demo
* Method: methodOfC
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_Demo_methodOfC
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
DemoImp.c
#include <jni.h>
#include "Demo.h"
#include <stdio.h>
//definition of methodOfC()
JNIEXPORT int JNICALL Java_Demo_methodOfC(JNIEnv* exeenv, jobject javaobj, int getFrmJava)
{
printf("This is in the C program\n Got %d from java",getFrmJava);
printf("\n.......Exiting frm C\n");
return getFrmJava+1;
}
Here is how I compiled and run my prog.: screenshot here
C:\native>javac Demo.java
C:\native>javah -jni Demo
C:\native>g++ -c -l"C:\Java\jdk1.6.0_26\include" -l"C:\Java\jdk1.6.0_26\include\win32" DemoImp.c
C:\native>g++ -shared DemoImp.o -o try.dll
C:\native>java Demo 1234
Control is in Java.......going to call a C program......
Congr8s no prob in CallApi.....
Exception in thread "main" java.lang.UnsatisfiedLinkError: Demo.methodOfC(I)I
at Demo.methodOfC(Native Method)
at Demo.main(Demo.java:23)
C:\native>
I've already added "C:\native" in my system path variable.
I've uploaded all my files in mediafire. Here's the link native.zip
If possible please tell me how can I make 64bit version of dll. Thanks in advance.
You have missed out the package name in the DemoImp.c file.
The naming convention for C function is Java_{package_and_classname}_{function_name}(JNI arguments). The dot in package name shall be replaced by underscore.

linking error when trying to use boost::asio

I think i'm getting crazy, im trying to compile a simple project to understand how to work with io_service and I cant compile it.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class testClass
{
unsigned int other_number;
unsigned int main_number;
boost::asio::io_service& io_serv;
public:
testClass(boost::asio::io_service& io) : other_number(0), io_serv(io), main_number(0){io_serv.post(boost::bind(&testClass::printNumbers, this));}
void changeNumber(int num)
{
io_serv.post(boost::bind(&testClass::doChangeNumber, this, num));
}
private:
void doChangeNumber(int num)
{
main_number = num;
}
void printNumbers()
{
std::cout<<"Main number is: "<<main_number<<" Other number is:"<<other_number<<std::endl;
other_number++;
Sleep(1000);
io_serv.post(boost::bind(&testClass::printNumbers, this));
}
};
void main()
{
boost::asio::io_service io_serv;
testClass tc(io_serv);
io_serv.run();
int num = 0;
while (true)
{
tc.changeNumber(num++);
Sleep(2000);
}
}
I did add in "project property->c/c++->general->additional include directories" the line: "C:\Program Files (x86)\boost_1_44_0";
And I did add in "project property->linker->additional library directories" the line: "C:\Program Files (x86)\boost_1_44_0\libs";
but nothing seems to work...
I'm using visual studio 2010..
there are no .lib files in boost_1_44_0\libs... I downloaded it 2 times from boost's site just to make sure..
no matter what I do, I always get LINK : fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-gd-1_44.lib'
You can build the Boost libs on your local system using bjam as described here (Section 5.2). Once you have done that you should be good to go - use this from a Visual Studio command prompt and make sure your project has the correct LIB path.
The prebuilt libs will only be there by default if you use the installer from Boost Pro Computing, I believe.

Resources